How to resolve PHP form repeat submission _php tips

Source: Internet
Author: User
Tags php form

Repeated submission is a problem we often encounter in development, in addition to using JS to prevent the recurrence of forms, but also can use PHP to prevent duplicate submissions Oh.

<?php
 /* * PHP How to prevent the form of duplicate
 submission
/session_start ();
if (Empty ($_session[' IP ')) {//First write operation to determine whether the IP address is logged, to know whether to write to the database
  $_session[' ip ' = $_server[' remote_addr ']; First write, a foreshadowing of the back refresh or back
  //...........//write the database operation
} else {//already has the first write operation, and then no longer write 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

 if (Isset ($token))

Token is included in the form as a 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 in this processing 
  //Header ("Location:". $_server[' php_self ']); 
} else { 
  //normal form submission, here processing 
  //echo "submitted";  
}

4. Set token value
$token = Mt_rand (0,1000000);
2$_session[' token ' = $token;

The above is about how to solve the PHP form to repeat the implementation method of submission, I hope to help you learn.

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.