Introduction to PHP Resolve form repeat commit implementation steps

Source: Internet
Author: User
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;

  • Related Article

    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.