【问题标题】:how to solve sending email every time the page is refreshed [duplicate]每次刷新页面时如何解决发送电子邮件[重复]
【发布时间】:2019-05-20 15:42:53
【问题描述】:

这些表单在提交后工作。但是在刷新页面后邮件一次又一次地发送。

<?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 \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
    echo "Message sent successfully...";
}else {
    echo "Message could not be sent...";
}
?>

【问题讨论】:

  • 你不把这个放在 if(isset($_POST)) 中是什么?
  • @MagnusEriksson,对不起我的朋友。它显示了一些错误,网络问题。多条评论从来不是我的本意

标签: php forms sendmail


【解决方案1】:

有多种处理方式。

1) 请在邮件发送成功后编写以下代码。

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

2) 如果这对您不起作用,请使用元刷新方法:

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) 您也可以使用 cookie。 要使用 cookie,您必须创建一个具有当前时间戳值的令牌,并且每当您创建具有当前时间戳的 cookie 时,然后在页面刷新时比较这两个变量。但是 cookie 有以前的时间戳,而 token 有当前的时间戳,那么它将不匹配。

$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

【讨论】:

  • 先生你能告诉我如何使用cookies
  • 以上代码不工作
  • 请显示更新后的代码。
  • $to = "abc.com"; $subject = "这是主题"; $message = "这是 HTML 消息。"; $message .= "

    这是标题。

    "; $header = "来自:mithlesh@rightturn.co.in \r\n"; $header .= "MIME 版本:1.0\r\n"; $header .= "内容类型:文本/html\r\n"; $retval = 邮件 ($to,$subject,$message,$header); if( $retval == true ) { echo "消息发送成功..."; }else { echo "消息无法发送..."; }
【解决方案2】:

您需要查看submit

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

所以你的代码是:

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 \r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type: text/html\r\n";
    $retval = mail ($to,$subject,$message,$header);
    if( $retval == true ) {
       echo "Message sent successfully...";
    }else {
       echo "Message could not be sent...";
    }
}

【讨论】:

  • 如果用户重新加载的页面 POST 数据仍然有$_POST['submit'] 所以当用户重新加载时执行上述代码
  • 那你得用Ajax
  • 先生,当我运行此代码时不起作用
  • 你能把你的表格给我看看吗?
  • 或发送后清除$_POST = array()
【解决方案3】:

大多数邮件发送发生在特定事件或活动之后,就像表单提交或按钮单击一样。您需要设计具有相关字段的表单。然后您可以使用 isset() 或 !empty() 用于 $_POST 数组

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 \r\n";
     $header .= "MIME-Version: 1.0\r\n";
     $header .= "Content-type: text/html\r\n";
     $retval = mail ($to,$subject,$message,$header);
     if( $retval == true ) {
        echo "Message sent successfully...";
     }else {
        echo "Message could not be sent...";
     }
}

【讨论】:

  • 刷新页面后仍有post数据
  • 在您的代码中,您没有使用表单处理或后数组..您必须先在任何 php 教程中检查它
【解决方案4】:

将此代码放在 isset 提交按钮之外的 php 代码的开头

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

它为 Session 设置了一个唯一的随机密钥。现在在表单字段中取一个隐藏字段

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

现在提交

首先检查当前$mykey 是否与隐藏变量匹配 执行某些操作后未设置$mykey 所以它可以防止用户通过刷新页面多次添加数据。

希望对你有帮助... :-)

【讨论】:

  • 不,我想要表单字段
  • 我不想要表单域
  • 您可以将此标记为答案
猜你喜欢
  • 2015-01-01
  • 2011-06-25
  • 2017-07-08
  • 2021-01-26
  • 2017-12-01
  • 2020-05-15
  • 1970-01-01
  • 2019-08-31
  • 2013-05-26
相关资源
最近更新 更多