Jack.Straw Posted November 4, 2006 Share Posted November 4, 2006 Hi. I'm learning PHP and can't seem to figure out how to use cookies properly. Can anyone tell me what i'm doing wrong here?[code]<?if (isset($_COOKIE["user"])) $user=$_COOKIE["user"];else $user="Guest"; $name="Jack; $life="36000"; setcookie('user', $name, $life);?> <html><head><title>.</title></head><body><? echo "Hello $user"; ?></body></html>[/code]It's supposed to say "Hello Guest" the first time you visit, then say "Hello Jack" when you reload the page. Can anyone tell me why?Thanks in advance!-Jack Link to comment https://forums.phpfreaks.com/topic/26173-trouble-learning-to-use-cookies/ Share on other sites More sharing options...
toplay Posted November 4, 2006 Share Posted November 4, 2006 Change:$name="Jack;$life="36000";to:$name="Jack";$life=36000;You have a syntax error. So, make sure that you are displaying errors and error reporting is set. Link to comment https://forums.phpfreaks.com/topic/26173-trouble-learning-to-use-cookies/#findComment-119694 Share on other sites More sharing options...
Jack.Straw Posted November 4, 2006 Author Share Posted November 4, 2006 Sorry about the typo's... fixed that. Actually, those were mistakes i made while editing the post to remove my real name. Anyway, error messages are on (believe me, i've been getting enough!)... it just always displays me as 'Guest'. Link to comment https://forums.phpfreaks.com/topic/26173-trouble-learning-to-use-cookies/#findComment-119712 Share on other sites More sharing options...
Psycho Posted November 4, 2006 Share Posted November 4, 2006 The proplem is in how you are defining the life value. Check the manual: http://us3.php.net/manual/en/function.setcookie.phpIt states "Expire: The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch."The "epoch" is some time in 1970 I think. So, you have set the expiration to be 36,000 seconds after that time. So, the cookie is expired as soon as you set it. Try setting:$life = time()+36000;That will make the cookie expire in 36,000 seconds from the time it is set - or 10 hours. Link to comment https://forums.phpfreaks.com/topic/26173-trouble-learning-to-use-cookies/#findComment-119718 Share on other sites More sharing options...
Jack.Straw Posted November 4, 2006 Author Share Posted November 4, 2006 That was it. Thank you so much for your time! Link to comment https://forums.phpfreaks.com/topic/26173-trouble-learning-to-use-cookies/#findComment-119727 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.