Search code examples
phpsetcookie

PHP 5.5 setcookie localhost not working


I've been working on a project on another operating system and it works fine there. I'm now on Arch Linux and now the project isn't working. The problem seems to be that my browser is not accepting a cookie.

I'm setting it with

setcookie('name','value', 0, '/', '', 0, true);

Firebug shows the cookie being sent and the function is returning true. But it the cookie isn't being used. This is on localhost.

Edit: I've also tried many combinations, like:

setcookie('test', 'value');
setcookie('test', 'value', 0, '', '', 0, true);

None seem to work.


Solution

  • Try this, sets cookie

    $time=time();
    setcookie("test", "value", time()+86400);
    

    The time can be adjusted it's set for one day before it expires.

    To read the cookie

    $varname = $_COOKIE["test"]; 
    echo $varname;
    

    It should echo out value since that is the example data your setting in cookie.

    You can also use the same name for setcookie to override your existing value