Page 1 of 1

Cookie testing

Posted: Sat Jun 04, 2005 9:06 pm
by MathewByrne
Hi,
I'm writing a Poll script at the moment. When someone clicks on Vote, the forms uses POST to the same page. When the page loads it checks whether POST data exists and sets a "Voted" Cookie if it has. Then later in the same script a display class checks whether the Cookie has been set.
The problem is that it doesn't recognise that the cookie has been set until the page is refreshed again. Is there any way to set the Cookie AND check if it has been set in the same script?

Posted: Sat Jun 04, 2005 9:11 pm
by Ambush Commander
Cookies, in my opinion, aren't the best way to remember whether or not someone has voted. If you've got a username to fix it to, add that to your database.

Considering your problem, it seems like you want to send and recieve your cookie at the same time. What you should do? Stop using superglobals throughout your script: you should be assigning them to a regular variable and doing input validation on them. If that's the case, all you have to do is set the value of the cookie to what every you just set the cookie to.

Posted: Sun Jun 05, 2005 12:26 am
by hongco
if you still insisted on using cookie for it. when submitting the post, have another file, says file B, process the POST instead of the file with the voting options, says file A. The file B will perform all the necessary steps such as set cookies, update database, etc.....it then redirects the user back to the file A using referer. On file A, you always have a part to check for the existing of the cookie. If cookie is there, show the result of the voting, if not, show the option to allow the user to vote. This way, user does not need to refresh the page to see the result.

If you only allow members to vote, then do not use cookie