Home > Article > Backend Development > PHP solution to prevent repeated submission of forms
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>
<br>
The current form page is_submit is set to 0
SESSION_START(); $_SESSION['is_submit'] = 0;
<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>
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='history.go(-1);' href='javascript:void(0)'>返回</a>"; } else { echo "请不用重复提交<a href='index.php'>PHP+SESSION防止表单重复提交</a>"; } }
<br>
[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
The code is as follows | Copy code |
##< ?php /* |
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>
Copy code | |
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> |
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!