PHP method to solve and avoid repeated form submission

WBOY
Release: 2016-08-08 09:26:06
Original
1239 people have browsed it

Reprinted from: http://www.hlmblog.com/183.html

When submitting the form, the page may suddenly load slowly due to network speed, etc. If the user repeatedly clicks the submit button, many errors will be generated in the database. data, leading to uncontrollable situations.

For example, the following situation will cause the form to be submitted repeatedly:
Click the submit button twice.
Click the refresh button.
Use the browser back button to repeat the previous operation, resulting in repeated form submissions.
Use browser history to submit forms repeatedly.
Duplicate HTTP requests from the browser.
The webpage was maliciously refreshed.

Here are several solutions:
One: Use js to set the button to turn gray after being clicked

$(document).ready(function(){
  $(input:submit).click(){
      setTimeout(function(){obj.disabled=true;},100)
  };
});
Copy after login

Two: Use session

to store the session (the tag generated when the form is requested) in the hidden field of the form.

Use this method to check whether the flag value exists after receiving the form data, delete it first, and then process the data; if it does not exist, it means it has been submitted, and this submission is ignored.

//服务端生成随机数存入session, 分配至表单页
$data['sess_id'] = $_SESSION['sid'] = mt_rand(1000, 9999);
$this->load->view('form', $data);

//表单页隐藏域存放此session值
<input type="hidden" name="sid" value="<?=$sess_id; ?>">

//处理
if($_POST['sid'] != '' && $_POST['sid'] == $_SESSION['sid'])
{
 unset($_SESSION['sid']);

 echo '处理数据';
}
else
{
 echo '已提交过表单';
}
Copy after login

Three: Using cookies
The principle is similar to that of session, but once the user's browser disables cookies, this function will be invalid

if(isset($_POST['submit'])){
    setcookie("tempcookie","",time()+30);  
    header("Location:".$_SERVER[PHP_SELF]);exit();  
}
if(isset($_COOKIE["tempcookie"])){  
    setcookie("tempcookie","",0);echo "您已经提交过表单";  
}
Copy after login

Four: Using the header function to jump

Once the user clicks the submit button, processing After completing the data, jump to other pages

if (isset($_POST['submit'])) {
   header('location:success.php');//处理数据后,转向到其他页面
}
Copy after login

Five: Use the database to add constraints

Add unique constraints or create unique indexes directly in the database. Once the user is found to have submitted repeatedly, a warning or prompt will be thrown directly,
or just process it This is the most direct and effective method for submitting data for the first time. It requires careful consideration of the early database design and architecture

The above introduces PHP methods to solve and avoid repeated submission of form forms, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template