Page 1 of 1

Cookie Trouble

Posted: Thu Feb 09, 2012 10:28 pm
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?

Re: Cookie Trouble

Posted: Fri Feb 10, 2012 11:33 pm
by jacek
It looks like it would work to me (although a huge mess !), what is the problem exactly ?

Re: Cookie Trouble

Posted: Tue Feb 14, 2012 1:24 am
by wrichards8
It might just be my browser, but the cookie doesn't seem to expire and unset itself properly.

Re: Cookie Trouble

Posted: Tue Feb 14, 2012 1:17 pm
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.