Workaround for PHP to prevent forms from repeating submissions

Source: Internet
Author: User
This article is the content of PHP to prevent the recurrence of the form of the solution, and now share to everyone, the need for friends can refer to this article in the content of


Php+session prevent forms from repeating commits

index.php


Current form page Is_submit set to 0

Session_Start (); $_session[' is_submit '] = 0;
<form id= "Reg" action= "post.php" method= "POST" >     <p> User name: <input type= "text" class= "input" name= " Username "id=" user "></p>     <p> Secret &nbsp; code: <input type=" password "class=" input "name=" password "Id=" "Pass" ></p>     <p>e-mail:<input type= "text" class= "input" name= "email" id= "email" ></p >     <p><input type= "Submit" name= "Submit" class= "BTN" value= "Submit Registration"/></p> </form>

post.php

If the form is submitted, the current ' is_submit is 1, if the post.php is refreshed, then the else code will be executed

Session_Start (); if (Isset ($_post[' submit ')) {     if ($_session[' is_submit '] = = ' 0 ') {         $_session[' is_submit '] = ' 1 ';         echo "code block, what to do, code ... <a onclick= ' history.go ( -1); ' href= ' javascript:void (0) ' > Return </a>";     } else {         echo "Please do not repeat submit <a href= ' index.php ' >php+session prevent form duplicate submission </a>";     }}

Introduction to PHP to resolve form repeating commit implementation method

[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 below to copy code

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.

code as follows copy code

<?php
/*
* How to prevent duplicate submissions of forms in PHP
*/
Session_Start ();
if (Empty ($_session[' IP '))) {//First write operation, determine whether 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 to the database operation
} else {//already have the first write operation, will no longer write to the database
Echo ' Please do not refresh and back 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

<input type= "hidden" name= "token" value= "<?php echo $token;?>"/>


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";
7.}

4. Set the token value

The code is as follows Copy Code

1. $token = Mt_rand (0,1000000);
2.$_session[' token '] = $token;




This article links:http://makaidong.com/kenshinobiy/22427_350409.html


Reprint Please specify the Source: Workaround for PHP to prevent forms from repeating submissions

Source: Machadone Search (tel: 15110131480:makaidongzi qq:1130122167 Public Number: makaidong-com)
Welcome to share this article, reproduced please keep the source!

Related recommendations:

PHP prevents form repeat submission

PHP prevents forms from repeating commits and form expiration processing

A summary of several common methods of PHP preventing forms from repeating submissions

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.