Codeigniter,xampp cookies没有设置

I'm using the codeigniter with xampp on a windows 7 PC.

I'm trying to use codeigniter's built in cookies, but I can't seem to get my cookies to set/stay. I know that the cookie code is going off, it's just not actually saving.

Here's the cookie code:

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

After running this and on every page, I've included print_r($_COOKIE); to see any/all cookies that are being set, but nothing shows up.

Is there something I've missed?


According to the docs:

The expiration is set in seconds, which will be added to the current time. Do not include the time, but rather only the number of seconds from now that you wish the cookie to be valid. If the expiration is set to zero the cookie will only last as long as the browser is open.

So your code should be like this:

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

Also i recommend you to set domain name and cookie path in the config file.

dqz30992 我试着像你说的那样做。 我在config.php中设置了以下内容:$ config ['cookie_domain'] =“http:// localhost”; $ config ['cookie_path'] =“/”; 并更改为$ this-> input-> set_cookie('userID',$ userID,259200); 它仍然没有设置。 应该注意的是,我最初使用的方法在我的其他域上工作,它根本不适用于localhost
大约 7 年之前 回复

Here's the solution for anyone else that runs into this problem:

Cookies cannot be created on localhost, you'll need to use instead.

Go into CI's application/config/config.php and change any references to localhost you might have and change them instead to and do the same for the cookies. Set the following variables as well:

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

Then to store the cookie: $this->input->set_cookie('userID', $userID, 259200);

Csdn user default icon