PHP Resolve form Repeat commit

Source: Internet
Author: User

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

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.