Hello!
I have tried to solve this problem for a week now, on a small song voting script. 🙁
I have a mySQL database table with the fields id, ip, lastvote
The idea is to keep users from "flooding" with votes, so they have to wait for 60 seconds until they can vote again.
The ip number is stored in the database. When a user does vote there is a check if the ip-number is in the database, if it is, it checks if there has gone 60 seconds or not. If a vote is done, the ip number is saved(or updated if already existing), with the unixtime.
I have commented the code for better understandning.
The "error" now, is that a person can vote how often he/she likes, and there is no update of the ip-number, instead the ip- number just gets a new line in the tabe (so there is several rows with the same ip-number, there should not be more than one(since the update SQL query should work)
Would be very happy for any help.
$Link = mysql_connect("localhost", "xxxx", "yyyyxxxx
mysql_select_db("zzzz", $Link);
$ip=$_SERVER['REMOTE_ADDR'];
$timenow=time();
$Query = "SELECT * FROM songvotecheck WHERE ip='$ip'" or die (mysql_error());
$Result = mysql_query($Query) or die("Error: ".mysql_error()." in query: ".$Query);
$row=mysql_fetch_array($Result);
if(mysql_num_rows!=0){
//Hit on the ip number
if(($timenow-$row['lastvote'])<60){
//There has NOT been 60 seconds since the last vote for this ip- number
echo 'You have already voted, you have to wait a while until you vote again<p><A HREF="index.php">Continue</A>';
}else{
//More than 60 seconds has gone since the last vote for this ip-number, voting is okey
$Query = "UPDATE songvotecheck SET lastvote='$timenow' WHERE ip='$ip'" or die (mysql_error());
mysql_db_query ("modernidiot", $Query, $Link) or die (mysql_error());
$vote=1;
}
}
elseif(mysql_num_rows==0){
//No hit on the ip-number, voting is okey
$Query = "INSERT INTO songvotecheck(ip, lastvote) values('$ip', '$timenow')" or die (mysql_error());
mysql_db_query ("modernidiot", $Query, $Link) or die (mysql_error());
$vote=1;
}
mysql_close($Link);
if($vote==1){
The voting code here, this one works just fine
$vote=0;
?>
<A HREF="index.php">Thanks for the vote<p>Continue</A>
<?
}?>
Example from the database table:
id ip lastvote
13 193.150.240.xx 1053875911 (same ip-numer on all these)
14 193.150.240.xx 1053875981
15 193.150.240.xx 1053875987
16 193.150.240.xx 1053876804
17 193.150.240.xx 1053876817
As I wrote above, I would be happy for any help, feel free to ask anything, if I may have left anything out etc.