Cookie Trouble

Ask about a PHP problem here.
Post Reply
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

Cookie Trouble

Post by wrichards8 »

I have this poll code
<?php echo "Which is your favourite film?<br><form action='' method='post'>\n";
$query = mysql_query("SELECT movie, movieid, votes FROM moviepoll ORDER BY movieid");
while ($result = mysql_fetch_assoc($query))
{
	echo "<input type='radio' name='box' id='box", $result["movieid"], "' value='", $result["movie"], "'>";
	echo "<label for='box", $result["movieid"], "'>", $result["movie"], "</label> (currently has ", $result["votes"], " votes) <br>\n";
}
echo "<input type='submit'>\n</form>";
$movie = $_POST["box"];
if(isset($movie))
{
	$expire = time() +30;
	if(isset($_COOKIE["voted"]))
	{
		echo "Sorry, you've voted already";
	}
	else
	{
		$query = mysql_query("SELECT movie, movieid FROM moviepoll WHERE movie='$movie'");
		$result = mysql_fetch_assoc($query);
		$id = $result["movieid"];
		$name = $result["movie"];
		$sql = "INSERT INTO moviepoll (movieid, movie) VALUES ($id, '$name') ON DUPLICATE KEY UPDATE votes = votes + 1";
		mysql_query($sql);
		if(time() > $expire)
		{
			setcookie("voted", $name, time()-30);
		}
		setcookie("voted", $name, $expire);
	}
}	
?>
and am trying to use cookies to limit the user voting again for a while, but I can't seem to get the cookies worked out, can you help?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Cookie Trouble

Post by jacek »

It looks like it would work to me (although a huge mess !), what is the problem exactly ?
Image
wrichards8
Posts: 66
Joined: Thu Jan 12, 2012 3:54 pm
Contact:

Re: Cookie Trouble

Post by wrichards8 »

It might just be my browser, but the cookie doesn't seem to expire and unset itself properly.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Cookie Trouble

Post by jacek »

                if(time() > $expire)
                {
                        setcookie("voted", $name, time()-30);
                }
                setcookie("voted", $name, $expire);
ah, here you are unsetting the ciikie and then imediately setting it again.
Image
Post Reply