php Resolve form Repeat commit
Time: 2015-2-28 | Comments: 1 reviews | viewed 189 times | Tags: php, w3cui
repeating commits is a common problem in our development, in addition to the use of JS to prevent the form of repeated submissions, but also can use PHP to prevent duplicate submissions Oh.
<?php
/*
* 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 '];//First write, to the back of the decision to refresh or back to make a cushion
//...........//Write database Operations
} else {//has already had the first write operation and is no longer written to the database
Echo ' Please do not refresh and rewind ';//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
if (isset ($token))
tokens are included in the form as hidden.
<input type= "hidden" name= "token" value= "<?php echo $token;?>" />
3. If you are submitting a form repeatedly
if ($_session["token"]! = $token) {
//Do not allow duplicate submissions, this processing
//Header ("Location:". $_server[' php_self ');
} else {
//Normal form submission, here processing
//echo "submitted";
}
4. Set the token value
1. $token = Mt_rand (0,1000000);
2.$_session[' token '] = $token;
PHP Resolve form Repeat commit