Forum Moderators: coopster
I have a problem with setcookie in PHP.
I'm running XAMPP on Win XP SP2.
I have a PHP script which looks for a cookie and if not found, tries to set one and refresh the page. The script however enters an infinite loop as it cannot set the cookie.
Here's my code (cookie.php):
// CREATE COOKIE
if (empty($sessionid)) {
header("Refresh: 0;url=cookie.php");
setcookie ("sessionid", md5 (uniqid (rand())),
time()+7200);
exit();
}
Appreciate any suggestions as to why this is happening!?
Thanks,
Bingo
I`m guessing the problem is because you are calling the header function before the cookie is set, so it would keep looping. Try:
if (empty($_COOKIE['sessionid'])) {
setcookie ("sessionid", md5 (uniqid (rand())),
time()+7200);
header("Refresh: 0;url=cookie.php");
exit();
}
Also, note the use of the superglobal $_COOKIE. Thats just in case you have register globals OFF.
dc
// CREATE COOKIE
if (empty($sessionid)) {
setcookie ("sessionid", md5 (uniqid (rand())), time()+7200);
echo header("Refresh: 0;url=cookie.php");
exit();
}
Pretty sure it's server config.
Anyone out there using XAMPP (Apache 2.0) on local?
Thanks,
Bingo
[edited by: Bingo at 9:59 am (utc) on Oct. 10, 2006]
error_reporting(E_ALL); to the top of the script to find any more errors that you may have.