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

前端 未结 3 1472
醉梦人生
醉梦人生 2021-01-29 02:23

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.

相关标签:
3条回答
  • 2021-01-29 02:48

    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...
    
    0 讨论(0)
  • 2021-01-29 02:54

    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>
    
    0 讨论(0)
  • 2021-01-29 02:59

    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.

    0 讨论(0)
提交回复
热议问题