View Full Version : Cookies and PHP

01-29-2012, 04:36 AM
How come when I have the following code:

$countere = '1';
setcookie('counter',$counter,time() + (720)); // 86400 = 1 day

The time doesn't ever get set? I have verified this through my browser and by closing my browser. If it matters I am using firefox to see this.

The cookie says it expires: At end of session

I am then reading the cookie information like:


$counter = $_COOKIE["counter"];
echo $counter;
$newNumber = $counter +1;
setcookie("counter", "$newNumber");
echo $newNumber;
if ($newNumber >6)

echo "Sorry you need to wait 5 more minutes before logging in";
setcookie("counter", "$newNumber");

01-29-2012, 04:48 AM
I'm not sure what the algorithm is you're using, but did you really want the "e" here:

$countere = '1';
setcookie('counter',$counter,time() + (720)); // 86400 = 1 day


01-29-2012, 05:03 AM
Thanks I removed the spare 'e' and was hoping that might fix the issue but it didn't.

01-29-2012, 05:13 AM
You might want to check the return values from setcookie(), it returns success or failure of the call. Specifically the docs say it must be called before any output to the browser, so I'm guessing the calls that you make after the echo's aren't working.


01-29-2012, 05:15 AM
setcookie needs at least value set if you want to set time expiry too....


setcookie("cookiename","somevalue",time() + 60*60*24*30);

will set a cookie named cookiename with a value of somevalue expiring in 30 days