标头位置未按条件定位/重定向


Header location not locating/redirecting as per condition

这里我已经写了if…else条件与不同的头位置。

$a = "yahoo";
if($a == "yahoo")
{
header('location:http://www.yahoo.com');
}else{
header('location:http://www.gmail.com');
}
header('location:http://www.google.com');

问题:

在上面的代码中,如果条件为true,那么它也将重定向到google.com

根据我的看法,我认为它首先进入if条件,然后重定向到给定的位置,下面的其他代码将不会执行。

当我用每个标头写入exit()die()时,它就会工作。

问题

有人能告诉我它在哪里产生问题吗?

为什么没有exit()die()就不能工作?

"Location:"标头。它不仅将此标头发送回浏览器,而且还向浏览器返回REDIRECT(302)状态代码,除非已经设置了201或3xx状态代码。

<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>

您可以在php手册php标题

中阅读更多关于这方面的信息

您可以在文档的注释中找到:http://php.net/manual/en/function.header.php#85254

标头被自身覆盖。因此,如果你不停止执行你的代码,你的头会变为"谷歌",然后发送给用户。。