【问题标题】:php redirect issue after email sent发送电子邮件后的php重定向问题
【发布时间】:2013-06-28 09:59:24
【问题描述】:

我遇到了旧的标头问题,只是无法让它重定向我该如何解决?

警告:无法修改标头信息 - 标头已发送

<?php if (isset($_POST['email']))
{
  $content = '<html><body>';
  $content .= 'Email Address: '.$_POST['email'].'<br />';
  $content .= 'Enquiry Type: '.$_POST['enquiry'].'<br />';
  $content .= 'Message: '.$_POST['message'].'<br />';
  $content .= 'Telephone Number: '.$_POST['telephone'].'<br />';
  $content .= 'Preferred Contact Method:" '.$_POST['contact-method'] ;
  $content .= '</body></html>';
  $to = 'myemail*email.com';

$subject = 'Website Contact Form ';

$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if (mail($to, $subject, $content, $headers)) {
header('Location:form-submitted.php');
    }

}
?>
<!DOCTYPE html>
<html>

【问题讨论】:

  • 确保&lt;?php标签之前没有任何空行。
  • 在执行此代码之前,必须有something打印内容。这是显示在您正在使用的页面顶部的所有代码吗?

标签: php redirect header


【解决方案1】:

表示已经发送了header。 请试试这个

回显"&lt;meta http-equiv='refresh' content=\"0; url=form-submitted.php\"&gt;";

【讨论】:

    【解决方案2】:

    使用exit();标头函数之后。

    header('Location:form-submitted.php');
    exit();
    

    【讨论】:

      【解决方案3】:

      您有一个无效的电子邮件 ID

      $to = 'myemail*email.com';
      

      应该是

      $to = 'myemail@email.com';// @ in place of *
      

      所以,mail 将在上述情况下 return false

      【讨论】:

        【解决方案4】:

        把这一行放在下面:

         ob_start();
        

        在执行 PHP 文件的开头对我有用。

        【讨论】:

          【解决方案5】:
          //Put this code at the top of your page
          <?php ob_start(); ?>
          

          你的代码.....

          //Put this code at the bottom of your page
          <?php ob_flush(); ?>
          

          【讨论】:

            【解决方案6】:

            使用 JS

            if (mail($to, $subject, $content, $headers)) {   
            ?>
            <script>window.location.replace("http://your url/");</script>
            <?php 
            }
            

            【讨论】:

              【解决方案7】:

              解释如下:删除所有可能我打印到网站之前header("Location: site.php") 的内容。这是因为 http 标头始终位于请求或响应的顶部。这意味着如果您在设置标头之前回显或打印任何内容到响应中,则会导致如下结果:

              HTTP/1.1 404 Not Found
              Date: Sun, 18 Oct 2012 10:36:20 GMT
              Server: Apache/2.2.14 (Win32)
              Content-Length: 230
              <p>this tag should not be here</p>
              Content-Type: text/html; charset=iso-8859-1
              
              <!DOCTYPE HTML>
              <html>
              <head>
                 <title>My site</title>
              </head>
              <body>
                 <h1>Hello</h1>
                 <p>Lorem ipsum</p>
              </body>
              </html>
              

              如上所示,&lt;p&gt; 元素可能已经用 php 打印出来,现在已经破坏了 http 响应。任何你想用header(); 函数设置的东西都会抛出错误。

              我希望这会有所帮助:)

              【讨论】:

                猜你喜欢
                • 2015-10-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-06-09
                • 2012-12-27
                • 2018-01-17
                • 1970-01-01
                • 2021-04-03
                相关资源
                最近更新 更多