Codeigniter, xampp cookies not setting


Codeigniter, xampp cookies not setting

我在Windows7 PC上使用带有examplep的代码点火器。

我正在尝试使用codeigniter的内置cookie,但我似乎无法设置/保留我的cookie。我知道cookie代码正在消失,只是它实际上并没有保存。

这是cookie代码:

$this->input->set_cookie('userID', $userID, time()+259200, 'http://localhost', '/');

在运行完这个之后,在每个页面上,我都包含了print_r($_COOKIE);,以查看正在设置的任何/所有cookie,但什么都没有显示。

我错过了什么吗?

根据文档:

过期时间设置为秒,将添加到当前时间不包括时间,而只包括秒数从现在起,您希望cookie有效。如果过期如果设置为零,cookie只会在浏览器打开时持续。

所以你的代码应该是这样的:

$this->input->set_cookie('userID', $userID, 259200);

此外,我建议您在配置文件中设置域名和cookie路径。

以下是其他遇到此问题的人的解决方案:

Cookies无法在localhost上创建,您需要使用http://127.0.0.1

进入CI的application/config/config.php,更改对localhost的任何引用,改为http://127.0.0.1,并对cookie执行同样的操作。同时设置以下变量:

$config['cookie_domain']    = "127.0.0.1";
$config['cookie_path']      = "/";

然后存储cookie:$this->input->set_cookie('userID', $userID, 259200);