Search code examples
phpcodeignitercookiessetcookie

Cookie gets deleted on restarting browser


The cookie which i set in codeigniter gets deleted after i restart the browser. I'm setting up a cookie like:

$test_cookie = array(
                    'name'=>'test',
                    'value'=> 'test',
                    'expire'=> time() + 60*60*24*14
                );

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

The print_r($test_cookie) returns:

Array ( [name] => test [value] => test [expire] => 1309943188 ) 

Now i can print the cookie to make sure that the cookie is set:

$test_cookie= $this->input->cookie('test');
echo "<b> Cookie value: </b>". $test_cookie;

The cookie prints the value correctly.

However, if i restart the browser, i don't get the cookie value anymore. I've tried multiple browsers. With the var_dump, i get: bool(false) Why the cookie is getting deleted when browser restarts?

Thanks.


Solution

  • The CodeIgniter documentation says the expires value is added to the current time. So effectively the expires value in your case is time() + time() + 60*60*24*14. This may be beyond the 32 Bit integer limit and turn into a negative value. This in turn will result in a temporary cookie that's deleted upon closing the browser.

    $test_cookie = array(
      'name'=>'test',
      'value'=> 'test',
      'expire'=> 60*60*24*14
    );
    

    should work. I think.