• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
set_cookie problem with expire time


I set a persistent cookie in a helper file which works perfectly:

$cookie = array(
        'name'   => 'PersistentCookie',
        'value'  => $value,
        'expire' => time() + 31 * 24 * 60 * 60,
        'domain' => '.mywebsite.ch',
        'path'   => '/',
        'prefix' => '');

However, the cookie expires `At The End Of Session`, not at the requested time.

Thanks for your help.


Hello Ornis,
Try setting a cookie using pure php.
setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */

If this still doesn't work, let me know.

Two things:

1) CodeIgniter automatically adds in the current time() to the expiration. So do not add time() in yourself, only the number of seconds ahead of the current time when you'd like it to expire. So remove time() from your equation and that will help.

2) If you have set any of your cookie preferences in the application/config/config.php file to things BESIDES the default, but you set any of the above items in your array to the default, they will be overwritten to the items set in the config file.

For example, the following defaults are checked in the set_cookie function:
prefix: ''
domain: ''
path: '/'
secure: FALSE

If any of those items in your $cookie array are equal to those defaults, but they are NOT equal to the defaults in the config file, it'll take the value from your config file instead. So double check those, because it could be very easily overlooked.

Thanks Aken. Your hint solved the problem.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.