PHP 电子邮件表格不要转到 header 上提供的位置

PHP Email form don't go to the location provided on header

我有一个 php 文件,其格式为 html (contacts.php),它在提交另一个 php 文件 (contact_en.php) 后调用,如下图:

    <?php
        $to = "xxxx@xxxx.xx".","."xxxx@xxxx.xx";
        $nome = $_POST['name'];
        $email = $_POST['email'];
        $mensagem = $_POST['message'];
        $subject = "Contato do website EN";
        $headers = "From: xxxx@xxxx.xx";
        $body = "From: $nome \n\n Email: $email \n\n Message: $mensagem";
        $sent = mail($to, $subject, $body, $headers);

        if($sent) {
            header("Location:../en/success.php");
        } else {
            print "A system error has occurred, please try again later.";
        }
    ?>

我遇到的问题是代码发送了电子邮件,但它没有转到 success.php,而是停留在 contact_en.php(空白页)。

我是 php 的新手,我该如何解决?

感谢您的帮助:)

如果在 header 之前使用 echo 语句,则不会重定向页面。所以你也可以试试这个。回声<script>window.location='url'</script>

要设置正确的 Location-header,您必须遵循一些规则。根据浏览器的不同,它可能不遵循这些也能正常工作,但它更安全:

  1. 使用带有方案、域、端口、路径等的绝对 URI http://www.domain.com/en/success.php
  2. 冒号和正文之间的space:header("Location: http://...");
  3. header()
  4. 之后添加exit;

所以最后用这个(可能路径要调整一下):

if($sent) {
    header("Location: http://".$_SERVER['HTTP_HOST']."/en/success.php");
    exit;
} else {
    print "A system error has occurred, please try again later.";
}