Home > Backend Development > PHP Tutorial > Use session in php to avoid repeated submission of forms when refreshing the page

Use session in php to avoid repeated submission of forms when refreshing the page

WBOY
Release: 2016-07-29 09:00:32
Original
1055 people have browsed it

(1) PHP part

session_start();
$sign=$_SESSION['code'];
$sscode=mt_rand(0,1000000);
$this->assign("sscode",$sscode);//将随机值传入html中
if(isset($_POST['submit'])){
    //isset($_POST['submit'])为判断是否有表单提交
    //$_POST['submit_sign']为表单中隐藏域的一个值
    if($_POST['submit_sign']==$sign){
        //处理表单数据  
    }
else{
    echo "<script>alert('请勿重复提交表单')</script>"
}
}</span>
Copy after login

(2) HTML part

<form action="" name="myform" method="post">
<input type="submit" value="提交" name="submit"/>
<input type="hidden" value="<?php echo $sscode?>" name=submit_sign/><!--隐藏域-->
</form></span>
Copy after login

The above introduces the use of session in PHP to avoid repeated submission of forms when refreshing the page, 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