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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

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 Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools