PHP solution to prevent repeated submission of forms

不言
Release: 2023-03-23 08:26:01
Original
1184 people have browsed it

The content of this article is a solution to prevent repeated submission of forms in PHP. Now I share it with everyone. Friends in need can refer to the content in this article

<br>

PHP+SESSION prevents repeated submission of forms

index.php

<br>

The current form page is_submit is set to 0

SESSION_START(); $_SESSION['is_submit'] = 0;
Copy after login
<form id="reg" action="post.php" method="post">     <p>用户名:<input type="text" class="input" name="username" id="user"></p>     <p>密   码:<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="提交注册"/></p> </form>
Copy after login

post.php

If the form is submitted, set the current 'is_submit to 1, if 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 "代码块,要做的事,代码...<a onclick=&#39;history.go(-1);&#39; href=&#39;javascript:void(0)&#39;>返回</a>";     } else {         echo "请不用重复提交<a href=&#39;index.php&#39;>PHP+SESSION防止表单重复提交</a>";     } }
Copy after login

<br>
Copy after login
Copy after login

php Introduction to the implementation method of solving repeated submission of forms

[Introduction] Repeated submission is a problem that we often encounter in development, except We use js to prevent repeated submissions of the form, and we can also use php to prevent repeated submissions. Example 1 The code is as follows Copy code

Repeated submission is a problem we often encounter in development. In addition to using js to prevent repeated submission of forms, we can also use php to prevent repeated submission. .

Example 1

<br>Specific principle

session scope variable token to prevent.

1. Open session:

session_start();

2. If there is a form submission

The code is as follows Copy code
##< ?php

/*
* How to prevent repeated submission of forms in php
*/
session_start();
if (empty($_SESSION['ip'])) {// For the first write operation, determine whether the IP address is recorded, so as to know whether to write to the database
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR']; //First write, for To pave the way for the subsequent judgment of refreshing or retreating
//...........//Writing to the database operation
} else {//There is already an operation after the first write, It will no longer be written to the database
echo 'Please do not refresh and back off again'; //Write some prompts or other things that have been written
}
?><br>

The code is as follows Copy code

if (isset($token))

<br>Token is included in the form in hidden form.

The code is as follows Copy code
##

3. If the form is submitted repeatedly<br>

The code is as followsCopy code##1.if ($_SESSION["token"] != $token) { 4. Set the token value
2. // Prevent duplicate submission, handle it here

3. // header("location:".$_SERVER['PHP_SELF']); <br>4.} else { <br>5. // Normal form submission, processed here <br>6. // echo "Submitted"; <br>7.}<br><br>

The code is as follows1 .$token = mt_rand(0,1000000);
<br>
Copy after login
Copy after login

<br>

<br>

此文链接:http://makaidong.com/kenshinobiy/22427_350409.html

<br>

转载请注明出处:PHP防止表单重复提交的解决方法

来源:马开东云搜索(电话:15110131480 微信:makaidongzi QQ:1130122167 微信公众号:makaidong-com)<br>       欢迎分享本文,转载请保留出处!

相关推荐:

php防止表单重复提交详解

PHP防止表单重复提交以及表单过期处理

PHP防止表单重复提交的几种常用方法汇总

Copy code
2.$_SESSION['token'] = $token;

<br>

The above is the detailed content of PHP solution to prevent repeated submission of forms. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!