How can I limit the number of times a single IP address submits a form?

Source: Internet
Author: User
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(
idInt (11) not null AUTO_INCREMENT COMMENT 'auto-incrementing id ',
ipChar (16) not null default '0' COMMENT 'IP address ',
form_idInt (11) not null default '0' COMMENT 'form id ',
last_submit_timeInt (11) not null default '0' comment' time of last submitted Form ',
success_submit_timesInt (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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.