flyersun
flyersun

Reputation: 917

codeigniter cookie expiry problem

I'm having a cookie issue, the expiry date on my cookie is always being set to At End Of Session which isn't what I want. I did a bit of goggling and it suggested it set the expire to time()+60*60*24*30 which I've done.

 //Create basket cookie
            $cookie = array(
                'name'   => 'basket_id',
                'value'  => $basket_id,
                'expire' => time()+60*60*24*30,
                'domain' => 'domain',
                'path'   => '/',
                'prefix' => '',
            );
            set_cookie($cookie);

I did wonder if it could be down to a Codeignter setting but my ci_session cookie has a normal expiry date. Thu, 09 Jun 2011 10:39:02 GMT

This is what I get when I view the cookie:

 Name   basket_id
 Value  28
 Host   .host
 Path   /
 Secure No
 Expires    At End Of Session

And here is an example of the array I'm passing to the cookie.

Array ( [name] => basket_id [value] => 30 [expire] => 1310202067 [domain] => host [path] => / [prefix] => ) 

Upvotes: 4

Views: 13434

Answers (3)

Dmytro Evseev
Dmytro Evseev

Reputation: 11581

Please check out the answer below by @Gowri for how to do it properly.

You can try to adjust session expiration time in config.php CI session initially is saved in cookies:

/** Session Variables
 ---------------------------------------
| 'session_expiration'  = the number of SECONDS you want the session to last.
|  by default sessions last 7200 seconds (two hours).  Set to zero for no expiration.
|
*/

$config['sess_expiration']      = 7200;

Upvotes: 3

Richard Li
Richard Li

Reputation: 47

You can add params

$config['cookie_lifetime']  = 1800

in config.php, the reason you can find in libraries/Sessions/Session.php, code below

$expiration = config_item('sess_expiration');

if (isset($params['cookie_lifetime']))
{
    $params['cookie_lifetime'] = (int) $params['cookie_lifetime'];
}
else
{
    $params['cookie_lifetime'] = (!isset($expiration) && config_item('sess_expire_on_close'))
        ? 0 : (int) $expiration;
}

Upvotes: 0

sn0r
sn0r

Reputation: 544

Your expiry date is set incorrectly. You don't have to include the time(), as what you're setting is actually the expiry date from time().

When you have an incorrect expire value, it defaults to 0, which is set as your session's length instead.

Therefore it should be:

            $cookie = array(
            'name'   => 'basket_id',
            'value'  => $basket_id,
            'expire' => 86400*30,
            'domain' => 'domain',
            'path'   => '/',
            'prefix' => '',
        );

Upvotes: 7

Related Questions