demsley Posted August 25, 2011 Posted August 25, 2011 Again feeling a bit of an idiot for having to ask this but I'm trying to work with Cookies and having problems. Code should work AFAICS but no; ------------------------------------------ <?php //Check and Increment Cookie if(!isset($_COOKIE['Counter'])) { echo "top section"; $Counter = $_COOKIE['Counter']+1; setcookie("Counter", $Counter ); echo "Setting Cookie to " . $Counter . ""; } else { echo "Cookie not set"; setcookie("Counter", "1" ); $Counter = 1; } ?> <?php // Print an individual cookie echo $_COOKIE["Counter"]; echo $HTTP_COOKIE_VARS["Counter"]; // Another way to debug/test is to view all cookies print_r($_COOKIE); ?> <?php // Print individual cookies echo "You have visited " . $Counter . " times "; ?> ------------------------------------- The page is available at: http://www.emsley.org.uk/poll/testcookies.php Any ideas would be gratefully received. Cheers Dave
localzuk Posted August 25, 2011 Posted August 25, 2011 Your if !isset is running the first section if the cookie is NOT set, and the else if it IS set. So they're backwards. Get rid of the ! 1
ZeroHour Posted August 25, 2011 Posted August 25, 2011 BTW unless you buffer you cant output/echo until you set your cookies. Any *output* ends the chance to set cookies so remove/move the echo's after the SetCookie. Normally an error would be in the logs for that. 1
demsley Posted September 2, 2011 Author Posted September 2, 2011 Thanks for the responses, I've taken out the echos I'd put in but it doesn't work still. I'm feeling very stupid at this stage and know there's something I've missed - but what it is I have no clue! <?php //Set Cookie if(isset($_COOKIE['Counter'])) { $Counter = $_COOKIE['Counter']+1; setcookie("Counter", $Counter ); } else { setcookie("Counter", "1" ); $Counter = 1; } ?> <?php // Print individual cookies echo "You have visited " . $Counter . " times "; ?>
CESIL Posted September 2, 2011 Posted September 2, 2011 Your Doctype is classed as output so you need to move this to after the cookie code above the xml line...
ZeroHour Posted September 2, 2011 Posted September 2, 2011 Your Doctype is classed as output so you need to move this to after the cookie code above the xml line... Yep the php code for cookies needs to literally be the first thing at the top of the file.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now