duanfan9859 2018-12-20 06:07
浏览 62

每次刷新页面时如何解决发送邮件的问题[重复]

This question already has an answer here:

These form work after submission .but after refreshing the page the mail send again and again.

<?php
$to = "abc.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:mithlesh@rightturn.co.in 
";
$header .= "MIME-Version: 1.0
";
$header .= "Content-type: text/html
";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
    echo "Message sent successfully...";
}else {
    echo "Message could not be sent...";
}
?>
</div>
  • 写回答

4条回答

  • dpblwmh5218 2018-12-20 06:12
    关注

    There are multiple ways to handle it.

    1) Please write below code after mail sent successfully.

    if(!$retval->Send()) {
        header("Location: http://www.example.com");
        exit;
    }
    

    2) If that does not work for you, use a meta refresh method:

    if(!$retval->Send()) {
        $to = "http://www.example.com";
        $url = $to;
        print "<meta HTTP-EQUIV=Refresh CONTENT=\"5; URL=$url\">";
        print "Thank you for your message.";
        exit;
    }
    

    3) You can use a cookie also. To use cookie,you have to create one token which have value of current timestamp and whenever you create cookie with also current timestamp and then compare both variable when page refresh. but cookies has previous timestamp and and token has current timestamp then it will not match.

    $token = time();
    setcookie('formToken', $token, time() + 3600);
    
    if(isset($_POST['submit'])){
    
        if($_POST['token'] != $_COOKIE['formToken']){
            // die("Sorry");
    
            $error_list .= '<li>You can not submit this form twice.</li>';
    
            echo $error_list;
            echo 'Thank you, your message has been sent. You do not need resubmit it again.';
            exit;
    
        }
    

    Source

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿