Question

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.

Was it helpful?

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

OTHER TIPS

I had the same issue. When I set it like this:

    setcookie("name","value",time()*3600);

and when I try to print it like this:

    echo $_COOKIE['name'];

it shows me a an error/warning on wamp that says "undefined index 'name'". When I change it to

    echo $_COOKIE["name"];

it works! Apparently, the double quotes and single quotes make a difference. Hope this helps!

Yeah, the setcookie('name','value', time()+86400), '/', 0, false); doesn't work on localhost machine, only the setcookie('name','value', time()+86400); seem to work. But when you try it on a real hosted domain the setcookie('name','value', time()+86400), '/', 0, false); works just fine. This has probably something to do with PHP versions or the local server you are currently using (XAMPP, WAMP, etc)

Try these snippets, works for me.

<?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
    foreach ($_COOKIE['cookie'] as $name => $value) {
        $name = htmlspecialchars($name);
        $value = htmlspecialchars($value);
        echo "$name : $value <br />\n";
    }
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top