How can I limit the number of times a single IP address submits a form? For example, I have developed a voting program that requires the same IP address to vote only once within two hours (that is, it is successfully submitted once ). How do I write code? (For Beginners, the question may be an idiot. Please be patient, thank you.) How can I limit the number of times a single IP address submits a form?
For example, I have developed a voting program that requires the same IP address to vote only once within two hours (that is, it is successfully submitted once ). How do I write code?
(For Beginners, you may be an idiot. Please be patient. Thank you)
Reply content:
How can I limit the number of times a single IP address submits a form?
For example, I have developed a voting program that requires the same IP address to vote only once within two hours (that is, it is successfully submitted once ). How do I write code?
(For Beginners, you may be an idiot. Please be patient. Thank you)
Follow the public account: phpgod (PHP Technology Daquan). Every day, we will share our stay.
Step 1: Create a table. DDL:
CREATE TABLEip_limit
(
id
Int (11) not null AUTO_INCREMENT COMMENT 'auto-incrementing id ',
ip
Char (16) not null default '0' COMMENT 'IP address ',
form_id
Int (11) not null default '0' COMMENT 'form id ',
last_submit_time
Int (11) not null default '0' comment' time of last submitted Form ',
success_submit_times
Int (11) not null default '0' comment' successful submissions ',
Primary key (id
)
) ENGINE = InnoDB default charset = utf8;
Step 2: The requirement logic is described as follows:
When you perform the first submission, insert a record to the ip_limit table, and remember the fields such as ip, form_id, last_submit_time, and success_submit_times. When you submit more requests, first, query the last_submit_time of the specified form_id for the corresponding ip address. If current_time-last_submit_time> 2*3600, update the last_submit_time and success_submit_times fields. Otherwise, related restrictions are prompted.
Save to redis and set the life cycle to 2 hours
The key is ip and the value is the number of visits.
The number of voting checks per time exceeds the returned error. The voting is allowed if the limit is not exceeded, and the number is increased by one.
Cache to files
Session
Redis and other memory databases
MySQL and other SQL databases
All can be implemented
There are advantages and disadvantages, speed, efficiency, and how to choose between them. Of course, it also depends on the service accuracy.
It is not reliable to use client javascript to limit the number of submissions. If someone directly modifies javascript, this restriction can be bypassed.
You can get the IP address of the client from the request of the server, and then you will know how to do it. However, this method is problematic for some customers who use proxy to access the Internet.
Use cache for Detection