Page 1 of 1

setcookie doesn't work

PostPosted: 01. April 2008 18:29
by geagle
I have just installed XAMPP 1.6.6a under Win32 (XP home edition), and everything seem to install correctly. However, I've run into a problem with setcookie(), which doesn't set a cookie when I access the php code via localhost. The same code works properly when I upload it to a commercial server (changing the domain appropriately). Note that I also tried to set the header directly (code commented out), and that failed as well. The code is as follows:

<?php
setcookie("vegetable", "artichoke", time()+3600, "/", "localhost", 0);

//header ("Set-Cookie: vegetable=artichoke; expires=time()+3600; path=/; domain=localhost");
if (isset($_COOKIE["vegetable"])) {
echo "<p>Hello again, you have chosen: ".$_COOKIE['vegetable'].".</p>";
} else {
echo "<p>Hello you. This may be your first visit.</p>";
}
?>

I've also tired the example code from the PHP site for setcookie(), again without success.

A session cookie does get set if I use the following:

<?php
session_start();
echo "<p>Your session ID is ".session_id().".</p>";
?>

All help appreciated, and thanks.

doesn't work under MacOS either

PostPosted: 01. April 2008 21:20
by geagle
As a further test, I downloaded and installed XAMPP under MacOS, and the same behavior is observed - can't set a cookie using setcookie(), but a session cookie is set using session_start(). Unless my code is setup wrong, this sure seems like a bug to me.

let's try another way

PostPosted: 03. April 2008 03:37
by geagle
so there have been almost 70 views of my original post but no replies so i'll ask my question another way - has anyone successfully set cookies using xamp?

PostPosted: 03. April 2008 11:11
by Nobbie
Code: Select all
<?php
setcookie("vegetable", "artichoke", time()+3600, "/", "localhost", 0);

//header ("Set-Cookie: vegetable=artichoke; expires=time()+3600; path=/; domain=localhost");
if (isset($_COOKIE["vegetable"])) {
echo "<p>Hello again, you have chosen: ".$_COOKIE['vegetable'].".</p>";
} else {
echo "<p>Hello you. This may be your first visit.</p>";
}
?>


This code is incorrect - a Cookie is NOT set immediately and NOT WITHIN the scope of the script which sets the cookie (this is different to Session Variables). When you call that script next time (or another script on the same server), THEN the cookie can be evaluated. That means, a new cookie cannot be evaluated in the first visit (if it set in the first visit), but in all following visits. Thats the way cookies are working.

PostPosted: 03. April 2008 12:35
by Xardas der Dunkle
What is happened if you let the domain parameter empty?
Some Browsers have problems with it.

Xardas got it!

PostPosted: 03. April 2008 15:28
by geagle
Xardas - leaving the domain blank fixed the problem - for whatever reason, it now works with both IE and Firefox. I may try Safari on a Mac later, but it seems more to be XAMPP than the browser. Anyway, it works.

Nobbie - I guess I forgot to mention that I was actually looking at the cookies that were set in the browser after opening the page and none were being added. So the code is correct, in that if you refresh the page the if with the cookie is invoked.

Thanks to both for your help.