Jump to content

Recommended Posts

Posted

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

Posted

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 1
Posted

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 ";

 

 

?>

 

Posted
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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...