繁体   English   中英

PHP代码中的标头重定向将不起作用

[英]Header-redirection within php-code won't work

给我发电子邮件后,我想重定向用户。
除非我在email()方法之后放一些东西(例如header() -method),否则代码将运行。
有什么提示吗?

function process()
{
    $msg = "Form Contents: \n\n";
    foreach($this->fields as $key => $field)
        $msg .= "$key :  $field \n";

    $to = 'mymail@gmail.com';
    $subject = 'thesubject';
    $from = $this->fields['email'];

    mail($to, $subject, $msg, "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n");

    header('Location: thanks.html');
}

当我遇到类似的问题时,通常是在行尾缺少分号,或者像在这种情况下那样,缺少括号,因为foreach代码块应由{}阻止。 请参阅文档

您的位置应该是绝对的,而不是相对的。

header("Location: /myPath/thanks.html");
<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

就我而言,有那些引号。 做到了。

标头之后('location:....'); 您应该添加一个出口; 否则,脚本将一直运行直到结束。

header('Location: thanks.html');
exit;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM