Home >Backend Development >PHP Problem >How to limit users to submit only once in php

How to limit users to submit only once in php

藏色散人
藏色散人Original
2021-09-24 09:55:122456browse

The implementation method of php restricting users to only submit once: 1. Obtain the ip through "$ip=getenv('REMOTE_ADDR');" and use ip to limit; 2. Save an IP in the client's browser A COOKIE valid for one day is enough.

How to limit users to submit only once in php

#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

How to limit users to submit only once in php?

php limits the number of posts, for example, you can only post once a day:

First, if you use IP restrictions, you can only get the external IP of the other user, then all the people in the Internet cafe The IPs of the computers are the same, and only one computer in this Internet cafe can submit within a day. If your customers are mainly home users, you can use IP restrictions, otherwise IP restrictions will restrict many innocent people.

Method to obtain IP:

$ip=getenv('REMOTE_ADDR');

IP restrictions can still be broken. Home broadband users will get a new IP if they stop dialing again.

Confirm again that the server cannot obtain an intranet IP address such as 192.1.1.100.

Second, in addition to IP, you can also use COOKIE restrictions. Save a COOKIE valid for one day in the client's browser, and the program detects the COOKIE for restrictions. This method can solve the problem of IP restrictions that are too restrictive for Internet cafes, but it cannot solve the problem of smart customers clearing the browser COOKIE by themselves.

How to set a COOKIE that is valid for one day:

setcookie("TestCookie", $value, time()+24*3600);

Both of the above two methods have their own merits. You can combine them to make a slightly more reasonable one, but neither of them is perfect.

Related introduction:

Cookies are often used to identify users. A cookie is a small file that a server leaves on a user's computer. Each time the same computer requests a page through the browser, the cookie will be sent to the computer. With PHP, you can create and retrieve cookie values.

The setcookie() function is used to set cookies.

Note: The setcookie() function must be located before the 100db36a723c770d327fc0aef2ce13b1 tag.

Grammar

setcookie(name, value, expire, path, domain);

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to limit users to submit only once in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn