setcookie doesn't work
Posted: 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.
<?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.