2

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.

4
  • RTM: php.net/manual/en/function.setcookie.php your not setting expire right... If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes). or the secure flag[bool] Commented Aug 27, 2013 at 21:24
  • I seem to be setting it correctly. This works on a live server, on another development server, just not this particular development environment. Commented Aug 27, 2013 at 21:32
  • have you tried putting in the domain for the 5th parameter, such as localhost or 127.0.0.1? Commented Aug 27, 2013 at 21:38
  • Ryan, yes. I tried both, as well as NULL and '' and false. Commented Aug 27, 2013 at 21:44

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

5 Comments

That works. I tried this in my index.php at the top: if (isset($_COOKIE['test'])) { var_dump($_COOKIE['test']); exit; } else { setcookie("test", "value", time()+86400); exit; }
But I'm wondering why my other settings aren't being accepted? I'm not sending out any output other than the cookie. It works everywhere else....
httponly, setting domain; those in particular. Though perhaps its location in my program. I think it's a really weird problem. I'll have to do more research and comment here if I figure it out. Thanks for jump starting the testing.
@dirtymikeandtheboys I think what your trying to do is set the cookies on localhost? Not a actually domain? if so I believe cookies are associated with domain/ip only.
Ah! That's it. I reconfigured my PHP system to work with my client IP address, which was "::1"
2

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!

Comments

1

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)

2 Comments

You missed the domain parameter on "setcookie('name','value', time()+86400), '/', 0, false);". On Mac with MAMP and the following is working over localhost: setcookie("mycookie", "cookievalue", time()+ 86400, "/", "", false, true);
If your running on localhost, you may want to check that the output_buffering setting is not set to off in your php.ini file. Set it like so output_buffering = 4096 . This should fix it without having to fill in all the parameters of setcookie(); If your on MAMP, just go to File > Edit Template > PHP > (Your running version).
0

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";
    }
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.