Hi,

another newbie here and 1st-time post so hope I have posted this correctly and I hope someone can help because it is doing my head in...

Basically I have a cookie that gets set after a user submits an answer to a poll question. When the user hits the back button and tries to resubmit they can't because the cookie is recognised. Which is fine.

The problem is when the user refreshes their browser or restarts their browser the cookie is not recognised (or must be cleared).

I think it's because it is only getting set after the submit button is hit but how do I keep the cookie set every time after that within the user's browser??

if it helps here's the code I've got above my opening html tag:

setCookie("mypoll", $_POST['qid'], time() + 2592000);
if (isset($HTTP_COOKIE_VARS) && !empty($HTTP_COOKIE_VARS)) {
    if ($HTTP_COOKIE_VARS['mypoll'] && $HTTP_COOKIE_VARS['mypoll'] == $_POST['qid']) {
		$voted = "blah";
	}
	else {
		$voted = "";
	}
}

Any help or advice here would be much appreciated!!!
Regards,
Persist01.

Let's think about the logic of things:
First step, we check if the user already voted - we check if the cookie is already set (beware that this is not very secure way to check that a user already voted, since a user can manually delete the cookie on his computer - you should probably combine this with the storage of the user IP and time of vote in a mysql table or a flat file on server side!);
anyway, let's get back to where we were.

if (isset($_COOKIE['mypoll'])) {
 echo "You already voted!";
 }
else {
 //// user hasn't voted yet, so we display the form or process the form if the user pushed the submit
 // we check if the form was submitted
 if (isset($_POST['qid'])) { // if form was submitted
  $mypoll = $_POST['qid'];
  setcookie('mypoll', $mypoll , time()+2592000 );
  // here you should store the value of the vote, together with the IP of user and time, in some mysql table or other sort of storage method so you can process the results later
  echo "Thank you for your vote! Here are the results.....";
  }
 else {
  // the form was not submitted, so you should show it to the user
  // FORM FOR THE VOTE
  }
 }

This should do it.

Let's think about the logic of things:
First step, we check if the user already voted - we check if the cookie is already set (beware that this is not very secure way to check that a user already voted, since a user can manually delete the cookie on his computer - you should probably combine this with the storage of the user IP and time of vote in a mysql table or a flat file on server side!);
anyway, let's get back to where we were.

if (isset($_COOKIE['mypoll'])) {
 echo "You already voted!";
 }
else {
 //// user hasn't voted yet, so we display the form or process the form if the user pushed the submit
 // we check if the form was submitted
 if (isset($_POST['qid'])) { // if form was submitted
  $mypoll = $_POST['qid'];
  setcookie('mypoll', $mypoll , time()+2592000 );
  // here you should store the value of the vote, together with the IP of user and time, in some mysql table or other sort of storage method so you can process the results later
  echo "Thank you for your vote! Here are the results.....";
  }
 else {
  // the form was not submitted, so you should show it to the user
  // FORM FOR THE VOTE
  }
 }

This should do it.

Thanks johny_d I will give this a go. I was beginning to think I hadn't posted my first post here correctly :S

What makes this more "interesting" at my end is that I have the echo statements above (ie. "You have already voted" & "Thank you for your vote") embedded within table cells because I have content around the poll section that needs to be displayed regardless.

And I have to set the cookie before the html, head, body tags and any subsequent table cells don't I???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.