1

I was trying to figure out how isset() and empty() related to setcookie() and $_COOKIE[]. But I came upon a road-block on the way.

Here is my test.php

<?php

//initialize cookie
$expiry = time()+60*60*9000;
setcookie('name1', '4', $expiry, '/', '', '', TRUE);

if (isset ($_COOKIE['name1'])) {

    echo 'cookievalue ' . $_COOKIE['name1'];

} else {

    echo 'cookie value not set';

}


if (!empty ($_COOKIE['name1'])) {

    echo 'cookievalue ' . $_COOKIE['name1'];

} else {

    echo 'cookie value empty';

}

?>

Here is my test1.php

<?php

if (isset ($_COOKIE['name1'])) {

    echo 'cookievalue ' . $_COOKIE['name1'];

} else {

    echo 'cookie value not set';

}


if (!empty ($_COOKIE['name1'])) {

    echo 'cookievalue ' . $_COOKIE['name1'];

} else {

    echo 'cookie value empty';

}

echo 'cookievalue ' . $_COOKIE['name1'];


?>

When I first load test.php, and then test1.php, everything seems to work fine. That is, test1.php is able to read the $_COOKIE[] variable that was set in test.php via setcookie(). (Although, as expected, test.php had to be refreshed once before cookie values were output in test.php.)

However, if I close the browser, and reopen it, and then just run test1.php, I get an "Undefined Index" notice on name1 in $_COOKIE['name1'].

Why can't test1.php pick up the $_COOKIE variable defined before the browser was closed? The cookie should still be stored in the computer. Why can't it pull up the cookie value from it after closing and reopening the browser?

thanks_in_advance
  • 2,603
  • 6
  • 28
  • 44
  • have you tried another browser? –  Oct 07 '14 at 22:01
  • php's cookies are generally SESSION cookies, which means they're auto-deleted when the browser closes. if you want a persistent cookie, then you'll have to set the expiry time properly with `session_set_cookie_params()`: http://php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime – Marc B Oct 07 '14 at 22:07
  • @MarcB this setting is not overridden by the 'expiry' parameter in setcookie() ?? – thanks_in_advance Oct 07 '14 at 22:12
  • It should. Use something like firebug or httpfox to observe the headers going to/from the browser, and see if your cookie value is being sent back. If it's NOT, then there's something wonky with the options you're using in setcookie, or maybe your server v.s. browser time is WAY off and 60*60*9000 is actually in the past from the browser's perspective. – Marc B Oct 07 '14 at 22:16
  • @Dagon Thank you for your suggestion. It works in another browser (Firefox). It wasn't working in my Chrome browser (I suspect I have some anti-cookie extension on Chrome that's deleting the cookie -- or something like that) – thanks_in_advance Oct 07 '14 at 22:30
  • having the same issue in FF where im using file_get_contents to retrive json data. on fist load the cookie data isnt read, on refresh it works. this was marked as answered because doing it on a different browser worked, but what is the actual issue and solution? – roberthuttinger May 16 '15 at 13:01

2 Answers2

1

Answering my own question.

Thanks to @Dagon tried it using a different browser. It works in another browser (Firefox). It wasn't working in my Chrome browser (I suspect I have some anti-cookie extension on Chrome that's deleting the cookie -- or something like that).

thanks_in_advance
  • 2,603
  • 6
  • 28
  • 44
-1

You dont need isset() its as simple as

if ($_COOKIE['name1']) {
Dava Gordon
  • 192
  • 2
  • 8
  • while true, does not address the question –  Oct 07 '14 at 22:31
  • i rewrote test1.php without all your isset() and empty and just had if($_COOKIE['name1']) { and it worked fine read the cookie that was set without a problem – Dava Gordon Oct 07 '14 at 22:33