This article describes how to prevent repeated submission of forms by recording IP addresses in php. it compares and analyzes how to prevent repeated submission by recording IP addresses and how to prevent repeated submission through databases, it is a very practical technique for you to choose and use flexibly. you can refer to the example in this article to analyze how php records IP addresses to prevent repeated submission of forms. Share it with you for your reference. The specific analysis is as follows:
This principle is relatively simple. when the user submits the request for the first time, we record the IP address of the user. if the user submits the form again within a fixed period of time, the system will prompt repeated submission, this method is usually used in the top step to support such applications. it is a very bad choice to prevent repeated data submission.
The code is as follows:
The code is as follows:
<? Php
Session_start ();
If (empty ($ _ SESSION ['IP']) // The first write operation to determine whether the ip address is recorded and whether to write data to the database
{
$ _ SESSION ['IP'] = $ _ SERVER ['remote _ ADDR ']; // writes data for the first time, paving the way for refresh or rollback judgment.
Mysql_query ("insert into admin (id, name, age) VALUES (123, 'Yao Ming ', 25)"); // write to the database
}
Else // the operation after the first write will no longer be written to the database
{
Echo 'do not submit the form repeatedly or refresh the page'; // write some written prompts or other things
}
?>
There are also ways to do this:
1: generate a random code on the page, that is, the random code is different each time you submit it. verify the random code when you submit it!
2: when submitting, verify that if the data exists, it will not be submitted.
If you want to prevent repeated submission of inbound IP addresses, it is not the best solution. we can query the database to see if there are the same records and whether the IP addresses want to be processed again.
The code is as follows:
The code is as follows:
$ SQL = "select * from table name where buy_tel = 'telephony 'and IP =' $ IP'"; // and $ time-buy_date <60
$ Query = $ db-> query ($ SQL );
If ($ db-> rows ($ query ))
{
Echo ('script alert ("you have submitted it. please do not submit it again! "); Script ');
}
Else
{
// Perform the warehouse receiving operation
}
I hope this article will help you with PHP programming.