How to restrict votes per day by IP in Php+Mysql voting?

懵懂的女人 提交于 2019-12-02 08:37:52

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...

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>

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!