Hello I have this voting script attached it counts votes by IP address. Please how can I create a kind of time session on the IP addresses. "say 5 votes a day per IP. Voter has to wait another 24 hours before voting again. I know there are kind of questions like this. I have tried few, but I just can't get it to work. Thanks.
Update script;
<?php
include("config.php");
$ip=$_SERVER['REMOTE_ADDR'];
$add_time = new DateTime(null, new DateTimeZone('Europe/London'));
$time=$add_time->format('Y-m-d H:i:s');
$timeMinus = $add_time = - 60*1*1*1;
if($_POST['id'])
{
$id=$_POST['id'];
$id = mysqli_real_escape_String($bd, $id);
$ip_sql=mysqli_query($bd, "SELECT ip_add FROM voting_ip WHERE mes_id_fk='$id' AND ip_add='$ip' AND add_time>'$timeMinus'");
$count=mysqli_num_rows($ip_sql);
if($count<= 2)
{
$query = mysqli_query($bd, "UPDATE Messages SET down=down+1 WHERE mes_id=$id");
( $query);
$sql_in = mysqli_query($bd, "INSERT INTO voting_ip (mes_id_fk,ip_add) values ('$id','$ip')");
( $sql_in);
}
else
{
echo "<script>alert('You have already voted, wait for 24 hours and vote again.');</script>";
}
$result=mysqli_query($bd, "select down from Messages where mes_id='$id'");
$row=mysqli_fetch_array($result);
$down_value=$row['down'];
echo $down_value;
}
?>
the easiest way to limit the number of votes per time frame it to store a time-stamp when the person votes in your voting_ip table... then the next time they vote, count all records in your voting table with the persons Id whose time-stamp is greater than the (current time - 24 hours). if the count is >=5 votes.. display the message saying you already votes. its should just be a simple modification to your existing code.
your select should be modified to something like this:
and your insert should be something like
$now can be set in php using something like:
and then $nowMinus24Hours is just a variable = $now minus the 24 hours.
NOTE: you can do a SELECT Count(ip_add).... or Select count(*) (Select ..) to get the number of records as the result of your query.
you can add a timestamp column in 'voting_ip' table and set no unique keys.
then you can do the query to get last 5 records.
just subtract the time by latest record and last record
for example:
Consider the following... wherein I attempt to execute the same query over and over, at approximately 2-3 second intervals.