Introduction to PHP to resolve form repeating commit implementation method
Introduction to PHP to resolve form repeating commit implementation method
Source: Time: 2013-09-05 19:27:06 read:2198 Share to:3
[Guide] repeated submissions are often encountered in our development of a problem, in addition to our use of JS to prevent the form of repeated submissions, but also can use PHP to prevent duplicate submissions Oh. Example 1 Code replication code duplication is a common problem in our development, in addition to our use of JS to prevent the form of repeated submissions, but also use PHP to prevent duplicate submissions Oh.
Example 1
The code is as follows |
Copy Code |
/* * How to prevent duplicate submission of forms in PHP */ Session_Start (); if (Empty ($_session[' IP '))) {//First write operation to determine if the IP address is logged to know whether to write to the database $_session[' IP '] = $_server[' remote_addr ']; The first write, for the back of the decision to refresh or back to make a cushion ...//write a database operation } else {//has already had the first write operation and is no longer written to the database Echo ' Please do not refresh and rewind again '; Write some hints or other things that have been written } ?> |
Specific principles
Session range variable token to prevent.
1. Open session:
Session_Start ();
2. If a form is submitted
The code is as follows |
Copy Code |
if (Isset ($token)) |
Tokens are included in the form as hidden.
The code is as follows |
Copy Code |
|
3. If you are submitting a form repeatedly
The code is as follows |
Copy Code |
1.if ($_session["token"]! = $token) { 2.//Do not allow duplicate submissions, this processing 3.//Header ("Location:". $_server[' php_self '); 4.} else { 5.//Normal form submission, here processing 6.//echo "submitted"; 9.3 |
4. Set the token value
The code is as follows |
Copy Code |
1. $token = Mt_rand (0,1000000); 2.$_session[' token '] = $token; |