php 如何在 CodeIgniter 中检索 cookie 值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6409994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:18:19  来源:igfitidea点击:

How to retrieve cookie value in CodeIgniter?

phpcodeignitercookiessetcookie

提问by Roman

I can print the session values in codeigniter by print_r($this->session->userdata);How can I print the cookies in codeigniter? I have just set a cookie:

我可以通过print_r($this->session->userdata);如何在 codeigniter 中打印 cookie 来打印codeigniter 中的会话值?我刚刚设置了一个cookie:

$cookie = array(
          'name'   => 'test_cookie',
          'value'  => 'test',
          'domain' => '/',
          'secure' => TRUE
          );

$this->input->set_cookie($cookie); 

How can i print the above cookie?

我怎样才能打印上面的cookie?

回答by Repox

Look at the documentation: Codeigniter Cookie Helper Guide

查看文档:Codeigniter Cookie Helper Guide

It says that you should use $this->input->cookie()to retrieve a cookie:

它说你应该$this->input->cookie()用来检索cookie:

$this->input->cookie('test_cookie', TRUE);

回答by Michael L Watson

This worked for me on localhost, security might need tightened for server

这在本地主机上对我有用,服务器的安全性可能需要加强

$this->load->helper('cookie');     
$cookie = array(
                    'name'   => 'data',
                    'value'  => '23',
                    'expire' =>  86500,
                    'secure' => false
                );
                $this->input->set_cookie($cookie); 
                var_dump($this->input->cookie('data', false));  

Expire needs to be numeric, removed path and set secure to false

过期需要是数字,删除路径并将安全设置为false

回答by jason

If you are using google chrome use inspect element to see if the cookie has been set... I think you can do it in FF, but I haven't used FF in a while... I only had one issue with cookies and that was I was setting domain to my live domain... So I have my cookie code like this:

如果您正在使用谷歌浏览器,请使用检查元素来查看 cookie 是否已设置...我认为您可以在 FF 中执行此操作,但我有一段时间没有使用 FF...我只有一个 cookie 问题,并且那是我将域设置为我的实时域......所以我有这样的 cookie 代码:

        $this->load->helper('cookie');

         $cookie = array(
           'name'   => 'the_cookie',
           'value'  => 'test value here',
           'expire' => '15000000',
           'prefix' => ''
        );
        $this->input->set_cookie($cookie);

Here you can see it is showing up in Google Chrome "Inspect Element Tool"

在这里你可以看到它显示在谷歌浏览器的“检查元素工具”中

Google chrome displaying the_cookie value

谷歌浏览器显示 the_cookie 值

回答by Raj

'secure' => TRUE

This does not allow me to fetch the cookie.

这不允许我获取 cookie。



just set

刚刚设置

'secure' => FALSE 

and see it may work.

看看它可能会起作用。

回答by Masoud Mustamandi

setting the security => TRUEwill not allow to print the cookie value in local, it only grant access to secure connections only so it will not print anything in localhost for you unless you set the security => FALSEthan using codeigniter CI_Inputclass you can get the value of cookie

设置security => TRUE将不允许在本地打印 cookie 值,它仅授予对安全连接的访问​​权限,因此它不会为您在 localhost 中打印任何内容,除非您设置了security => FALSE使用 codeigniter CI_Input类,您可以获得 cookie 的值

$this->input->cookie('cookie_name', TRUE);  //with xss filtering 
$this->input->cookie('cookie_name');        //without xss filtering

回答by Vinit Kadkol

If the code mentioned below does not provide any output, then modify the application/config/config.phpfile and setting this:

如果下面提到的代码没有提供任何输出,那么修改 application/config/config.php文件并设置:

$config['global_xss_filtering'] = TRUE;

$this->input->cookie('cookie_name', TRUE);


else just use this it will display the value

否则只需使用它就会显示值

$this->input->cookie('cookie_name'); 

回答by Eddie

Load the cookie helper with:

加载 cookie 助手:

$this->load->helper('cookie');

Then retrieve your cooking with:

然后使用以下命令检索您的烹饪:

$cookieData = get_cookie("cookie_name");

Note, these are aliases to using the input class, you can also retrieve and set cookies like so:

请注意,这些是使用输入类的别名,您还可以像这样检索和设置 cookie:

$cookieData = $this->input->get_cookie("cookie_name");

Source http://ellislab.com/codeigniter/user-guide/helpers/cookie_helper.html

来源 http://ellislab.com/codeigniter/user-guide/helpers/cookie_helper.html