Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-29-2012, 03:36 AM   PM User | #1
golffor1
New Coder

 
Join Date: Feb 2008
Posts: 39
Thanks: 0
Thanked 1 Time in 1 Post
golffor1 is an unknown quantity at this point
Cookies and PHP

How come when I have the following code:

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:

Code:
 <?PHP

$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");
}
?>
golffor1 is offline   Reply With Quote
Old 01-29-2012, 03:48 AM   PM User | #2
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 891
Thanks: 4
Thanked 205 Times in 204 Posts
tracknut is an unknown quantity at this point
I'm not sure what the algorithm is you're using, but did you really want the "e" here:

Code:
$countere = '1';
setcookie('counter',$counter,time() + (720)); // 86400 = 1 day
Dave
tracknut is offline   Reply With Quote
Old 01-29-2012, 04:03 AM   PM User | #3
golffor1
New Coder

 
Join Date: Feb 2008
Posts: 39
Thanks: 0
Thanked 1 Time in 1 Post
golffor1 is an unknown quantity at this point
Thanks I removed the spare 'e' and was hoping that might fix the issue but it didn't.
golffor1 is offline   Reply With Quote
Old 01-29-2012, 04:13 AM   PM User | #4
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 891
Thanks: 4
Thanked 205 Times in 204 Posts
tracknut is an unknown quantity at this point
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.

Dave
tracknut is offline   Reply With Quote
Old 01-29-2012, 04:15 AM   PM User | #5
jmj001
Regular Coder

 
Join Date: Jan 2012
Posts: 271
Thanks: 2
Thanked 65 Times in 65 Posts
jmj001 is an unknown quantity at this point
setcookie needs at least value set if you want to set time expiry too....

eg;

PHP Code:
setcookie("cookiename","somevalue",time() + 60*60*24*30); 
will set a cookie named cookiename with a value of somevalue expiring in 30 days
jmj001 is offline   Reply With Quote
Reply

Bookmarks

Tags
cookies

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:36 AM.


Advertisement
Log in to turn off these ads.