setcookie doesn't work

Problems with the Windows version of XAMPP, questions, comments, and anything related.

setcookie doesn't work

Postby geagle » 01. April 2008 18:29

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.
geagle
 
Posts: 5
Joined: 01. April 2008 18:16

doesn't work under MacOS either

Postby geagle » 01. April 2008 21:20

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.
geagle
 
Posts: 5
Joined: 01. April 2008 18:16

let's try another way

Postby geagle » 03. April 2008 03:37

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?
geagle
 
Posts: 5
Joined: 01. April 2008 18:16

Postby Nobbie » 03. April 2008 11:11

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.
Nobbie
 
Posts: 8779
Joined: 09. March 2008 13:04

Postby Xardas der Dunkle » 03. April 2008 12:35

What is happened if you let the domain parameter empty?
Some Browsers have problems with it.
User avatar
Xardas der Dunkle
 
Posts: 482
Joined: 09. March 2008 19:40
Location: /var/www

Xardas got it!

Postby geagle » 03. April 2008 15:28

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.
geagle
 
Posts: 5
Joined: 01. April 2008 18:16


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 52 guests