Web Development Thread, Cookies in PHP in Coding and Web Development; Again feeling a bit of an idiot for having to ask this but I'm trying to work with Cookies and ...
-
25th August 2011, 12:56 PM #1 Cookies in PHP
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<br />";
$Counter = $_COOKIE['Counter']+1;
setcookie("Counter", $Counter );
echo "Setting Cookie to " . $Counter . "<br />";
}
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);
?>
<html>
<body>
<?php
// Print individual cookies
echo "You have visited " . $Counter . " times ";
?>
</body>
</html>
-------------------------------------
The page is available at: http://www.emsley.org.uk/poll/testcookies.php
Any ideas would be gratefully received.
Cheers
Dave
-
-
IDG Tech News
-
25th August 2011, 01:00 PM #2 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 !
-
Thanks to localzuk from:
demsley (2nd September 2011)
-
25th August 2011, 01:15 PM #3
-
Thanks to CESIL from:
demsley (2nd September 2011)
-
25th August 2011, 01:54 PM #4 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.
-
Thanks to ZeroHour from:
demsley (2nd September 2011)
-
2nd September 2011, 09:32 AM #5 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!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
//Set Cookie
if(isset($_COOKIE['Counter'])) {
$Counter = $_COOKIE['Counter']+1;
setcookie("Counter", $Counter );
}
else {
setcookie("Counter", "1" );
$Counter = 1;
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<?php
// Print individual cookies
echo "You have visited " . $Counter . " times ";
?>
</body>
</html>
-
-
2nd September 2011, 09:50 AM #6 Your Doctype is classed as output so you need to move this to after the cookie code above the xml line...
-
-
2nd September 2011, 11:24 AM #7 
Originally Posted by
CESIL
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.
-
SHARE: 
Similar Threads
-
By browolf in forum Coding
Replies: 3
Last Post: 5th July 2009, 12:07 AM
-
By Hightower in forum Web Development
Replies: 26
Last Post: 18th November 2008, 01:18 PM
-
By markwilliamson2001 in forum Web Development
Replies: 5
Last Post: 5th October 2007, 08:43 AM
-
By StewartKnight in forum Web Development
Replies: 18
Last Post: 21st February 2007, 08:53 AM
-
By woody in forum Web Development
Replies: 11
Last Post: 20th October 2006, 08:28 AM
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules