search
HomeBackend DevelopmentPHP ProblemHow to prevent repeated submission of forms in php

php prevents repeated submission of forms

How to prevent repeated submission of forms in php

Solution 1: Introduce cookie mechanism to solve

Submit the page code as follows a.php code is as follows: (Recommended learning: PHP programming from entry to proficiency)

<form id="form1" name="form1" method="post" action="t2.php">
    <p>说明
        <input type="text" name="titile" />
</p>
    <p>
        <input type="submit" name="Submit" value="提交" />
</p>
</form>
<?php
setcookie("onlypost", &#39;t&#39;);         //设置cookie,可以带上时间值。像有些论坛防止灌水就可以将你的一些基本信息存放到里面。
?>

Php code

<?php  
if($_COOKIE[&#39;onlypost&#39;] == &#39;t&#39;){  
    print_r($_COOKIE);  
    //处理提交的内容           如果验证成功则处理  
    print "ok";  
    setcookie("onlypost", &#39;f&#39;); //改变cooike值删除也可以了  
}  
?>

Disadvantages of the above processing: If the customer If cookies are disabled on the client, this method will have no effect. Please note this.

Solution 2: Use session (this is the same as JSP processing method)

Using PHP's Session function can also avoid repeated submission of forms. Session is saved on the server side. The Session variable can be changed while PHP is running. The next time you access this variable, you will get the newly assigned value. Therefore, you can use a Session variable to record the value submitted by the form. If it does not match, it is considered It is the user who is submitting repeatedly.

Code of page A:

<?php
session_start();                //根据当前SESSION生成随机数
$code = mt_rand(0,1000000);
$_SESSION[&#39;code&#39;] = $code;      //将此随机数暂存入到session
?>
<form id="form1" name="form1" method="post" action="t2.php">
    <p>说明
        <input type="text" name="titile" />
        <input type="hidden" name="originator" value="<?php echo $code;?>">
</p>
    <p>
        <input type="submit" name="Submit" value="提交" />
</p>
</form>
B页面:
<?php
session_start();
if(isset($_POST[&#39;originator&#39;])) {
    if($_POST[&#39;originator&#39;] == $_SESSION[&#39;code&#39;]){
        echo "ok";
        unset($_SESSION["code"]);               //将其清除掉此时再按F5则无效
    }else{
    echo "请不要刷新本页面或重复提交表单";
    }
}?>

Solution 3: Redirect processing on the server side

if (isset($_POST[&#39;action&#39;]) && $_POST[&#39;action&#39;] == &#39;submitted&#39;) {
// 处理数据,如插入数据后,立即转向到其他页面
header(&#39;location:submits_success.php&#39;);     //效果与JSP里面的sendRedirect类似
}

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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),