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

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>
douchunsui2395
douchunsui2395 对不起,朋友。它显示出一些错误,网络问题。多次评论从来都不是我的强项
一年多之前 回复
dongwei3120
dongwei3120 如果(isset($_POST))你怎么把它放进去?
一年多之前 回复

4个回答

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

douwei1930
douwei1930 $ to =“abc.com”; $ subject =“这是主题”; $ message =“<b>这是HTML消息。</ b>”; $ message。=“<h1>这是标题。</ h1>”; $ header =“来自:mithlesh@rightturn.co.in ”; $ header。=“MIME-Version:1.0 ”; $ header。=“Content-type:text / html ”; $ retval = mail($ to,$ subject,$ message,$ header); if($ retval == true){echo“消息发送成功...”; } else {echo“无法发送消息......”; }
一年多之前 回复
duan0513
duan0513 请显示更新的代码。
一年多之前 回复
duai8153
duai8153 上面的代码不起作用
一年多之前 回复
dousheyan0375
dousheyan0375 先生,您能告诉我如何使用cookies
一年多之前 回复

You need to check submit

if (isset($_POST['submit'])) {
}

So your code is :

if (isset($_POST['submit'])) {
    $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...";
    }
}
dszsajhd237437
dszsajhd237437 不,我不想要任何形式
一年多之前 回复
dongtuojuan8998
dongtuojuan8998 发送后清除$ _POST = array()
一年多之前 回复
dqb78642
dqb78642 你能告诉我你的表格吗?
一年多之前 回复
duangang4940
duangang4940 先生,当我运行此代码不起作用
一年多之前 回复
dqdz6464
dqdz6464 然后你必须使用Ajax
一年多之前 回复
donglu5047
donglu5047 如果用户重新加载页面POST数据仍然有$ _POST ['submit']那么每当用户重新加载时执行Above Code
一年多之前 回复

Mostly mail sending happens after particular event or activity just like form-submit or button-click.. you need to design form with relevant fields.. then you can use isset() or !empty() for $_POST array

if(!empty($_POST)){
    $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...";
     }
}
douji4948
douji4948 在你的代码中,你没有使用表单处理或后期数组..你必须先在任何php教程中检查它
一年多之前 回复
douguanyan9928
douguanyan9928 刷新页面后仍然有帖子数据
一年多之前 回复

put this code in starting of your php code outside isset submit button

if(empty($_SESSION['key']) && !isset($_SESSION['key'])){
  $randomkey = rand(0,99999);
  $mykey = $_SESSION['key'] = $randomkey
}

It set Session a unique random key. now in form field take one hidden field

<input type="hidden" name="key" value="<?=$mykey?>">

Now On Submit

First Check Current $mykey Matches With Hidden Variable After Performing Certain Action Unset $mykey So it prevent user adding data multiple times by Refreshing the page.

I hope it Helps... :-)

dsh77114
dsh77114 你可以将其标记为答案
一年多之前 回复
doudou_0624
doudou_0624 我不想要一个表格领域
一年多之前 回复
douzhi9635
douzhi9635 不,我想要表格领域
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问

相似问题

2
如何解决ViewPager+Fragment+GridView实现无限滑动时页面切换的卡顿问题?
1
a标签绑定下载文件的链接如何实现不刷新页面呢a标签绑定下载文件的链接如何实现不刷新页面呢
1
微信分享的ticket是一样的吗?我每次刷新值都是一样的!
0
js websocket长连接刷新页面问题
3
JavaScript的window.location.reload()方法重复不停刷新
0
MUI下拉刷新与区域滚动冲突的解决办法?
1
pycharm + django template 页面修改之后重新刷新,修改不生效,每次都需要重启项目?如何解决啊?
1
html页面刷新时怎样让内容慢慢显示
0
nuxt asyncData 中 store参数在刷新页面后会丢失的问题 有人知道吗?
3
关于页面点击事件 页面强制刷新 根目录多个问号
1
datagridview动态添加数据后,如何保证选中的单元格还在刷新之前的位置?
1
springboot项目集成springsession+redis后,刷新页面会导致session丢失
4
如何在微信公众号中,刷新页面数据
2
vue 左侧菜单出现重复菜单问题怎么解决? 有遇到过这样的问题吗?
2
关于每次刷新都能加载不同的新数据的问题?
0
登陆outlook邮箱的网页版,为什么会显示“:-( 发生错误 正在访问的邮箱的帐户状态无效(“ProtocolDisabled”)。 更多详细信息... 刷新页面”,一直打不开邮箱网页版是怎么回事?如何解决?
1
layui弹窗前如何刷新网页
2
.net 页面跳转刷新问题。
0
用户登录页面刷新时不按提交按钮时会提交表单