问题
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;
}
?>
回答1:
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:
$times=mysqli_query($bd, "SELECT timestamp FROM voting_ip WHERE mes_id_fk='$id' AND ip_add='$ip' order by timestamp desc limit 5");
if(mysqli_num_rows($ip_sql) < 5 || {first record - last record < 24 hours})
...your codes...
回答2:
Consider the following... wherein I attempt to execute the same query over and over, at approximately 2-3 second intervals.
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(dt DATETIME NOT NULL);
SELECT * FROM my_table;
+---------------------+
| dt |
+---------------------+
| 2016-03-28 22:35:57 |
| 2016-03-28 22:36:06 |
+---------------------+
2 rows in set (0.00 sec)
mysql> INSERT INTO my_table (dt) SELECT DISTINCT NOW() FROM (SELECT 1) a LEFT JOIN my_table b ON NOW() < b.dt + INTERVAL 10 SECOND WHERE b.dt IS NULL;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM my_table;
+---------------------+
| dt |
+---------------------+
| 2016-03-28 22:35:57 |
| 2016-03-28 22:36:06 |
| 2016-03-28 22:37:27 |
+---------------------+
3 rows in set (0.00 sec)
mysql> INSERT INTO my_table (dt) SELECT DISTINCT NOW() FROM (SELECT 1) a LEFT JOIN my_table b ON NOW() < b.dt + INTERVAL 10 SECOND WHERE b.dt IS NULL;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM my_table;
+---------------------+
| dt |
+---------------------+
| 2016-03-28 22:35:57 |
| 2016-03-28 22:36:06 |
| 2016-03-28 22:37:27 |
+---------------------+
3 rows in set (0.00 sec)
mysql> INSERT INTO my_table (dt) SELECT DISTINCT NOW() FROM (SELECT 1) a LEFT JOIN my_table b ON NOW() < b.dt + INTERVAL 10 SECOND WHERE b.dt IS NULL;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM my_table;
+---------------------+
| dt |
+---------------------+
| 2016-03-28 22:35:57 |
| 2016-03-28 22:36:06 |
| 2016-03-28 22:37:27 |
+---------------------+
3 rows in set (0.00 sec)
mysql> INSERT INTO my_table (dt) SELECT DISTINCT NOW() FROM (SELECT 1) a LEFT JOIN my_table b ON NOW() < b.dt + INTERVAL 10 SECOND WHERE b.dt IS NULL;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM my_table;
+---------------------+
| dt |
+---------------------+
| 2016-03-28 22:35:57 |
| 2016-03-28 22:36:06 |
| 2016-03-28 22:37:27 |
| 2016-03-28 22:37:37 |
+---------------------+
4 rows in set (0.00 sec)
mysql> INSERT INTO my_table (dt) SELECT DISTINCT NOW() FROM (SELECT 1) a LEFT JOIN my_table b ON NOW() < b.dt + INTERVAL 10 SECOND WHERE b.dt IS NULL;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM my_table;
+---------------------+
| dt |
+---------------------+
| 2016-03-28 22:35:57 |
| 2016-03-28 22:36:06 |
| 2016-03-28 22:37:27 |
| 2016-03-28 22:37:37 |
+---------------------+
4 rows in set (0.00 sec)
mysql> INSERT INTO my_table (dt) SELECT DISTINCT NOW() FROM (SELECT 1) a LEFT JOIN my_table b ON NOW() < b.dt + INTERVAL 10 SECOND WHERE b.dt IS NULL;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM my_table;
+---------------------+
| dt |
+---------------------+
| 2016-03-28 22:35:57 |
| 2016-03-28 22:36:06 |
| 2016-03-28 22:37:27 |
| 2016-03-28 22:37:37 |
+---------------------+
4 rows in set (0.00 sec)
mysql> INSERT INTO my_table (dt) SELECT DISTINCT NOW() FROM (SELECT 1) a LEFT JOIN my_table b ON NOW() < b.dt + INTERVAL 10 SECOND WHERE b.dt IS NULL;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM my_table;
+---------------------+
| dt |
+---------------------+
| 2016-03-28 22:35:57 |
| 2016-03-28 22:36:06 |
| 2016-03-28 22:37:27 |
| 2016-03-28 22:37:37 |
| 2016-03-28 22:37:47 |
+---------------------+
5 rows in set (0.00 sec)
mysql>
回答3:
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:
SELECT ip_add FROM voting_ip
WHERE mes_id_fk='$id' AND ip_add='$ip' AND timetamp>'$nowMinus24Hours'
and your insert should be something like
INSERT INTO voting_ip (mes_id_fk,ip_add,timestamp) values ('$id','$ip','$now')
$now can be set in php using something like:
$timestamp = new DateTime();
$now=$timestamp->format('Y-m-d H:i:s');
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.
来源:https://stackoverflow.com/questions/36270561/how-to-restrict-votes-per-day-by-ip-in-phpmysql-voting