Forum Moderators: coopster

Message Too Old, No Replies

PHP Setcookie problem

         

Bingo

9:25 am on Oct 10, 2006 (gmt 0)

10+ Year Member



Hi,

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

dreamcatcher

9:50 am on Oct 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi 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

Bingo

9:58 am on Oct 10, 2006 (gmt 0)

10+ Year Member



Thanks dreamcatcher, but just realised I pasted the code wrong! It is in fact:


// 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]

Bingo

9:49 am on Oct 11, 2006 (gmt 0)

10+ Year Member



<<Bump>>

Can anyone offer any help on this?

Thanks.

eelixduppy

10:59 am on Oct 11, 2006 (gmt 0)



You have an error with your header. Refer to the HTTP/1.1 specification [faqs.org] for more details on this. Also, add
error_reporting(E_ALL);
to the top of the script to find any more errors that you may have.