如何在PHP中声明多个头信息

12

我想要根据用户的操作将其发送到不同的页面。因此,我在页面顶部创建了多个函数,如下所示:

<?php

function one() {
     header("location: pagea.php");
}
function two() {
     header("location: pageb.php");
}
function three() {
     header("location: pagec.php");
}

?>

当然会出现错误,因为我在重新声明headers。一开始我还以为这样做没问题,因为我将它们包含在函数内,每次只调用一个函数。但是仍然出现了错误。有没有其他方法可以解决这个问题?


你得到了什么错误?这段代码看起来是合法的。 - jwhat
你能否添加代码,演示如何按顺序调用函数 one、two 和 three? - Luca Rocchi
9个回答

22

我认为您误解了HTTP头Location的作用。

Location头指示客户端导航到另一个页面。每个页面只能发送一个Location头。

此外,PHP在第一次输出之前会发送标头。一旦您输出了内容,就不能再指定任何标头(除非您使用输出缓冲)。

如果您两次指定相同的标头,默认情况下,header()将用最新值替换先前的值... 例如:

<?php
header('Location: a.php');
header('Location: b.php');
header('Location: c.php');

将会重定向用户到c.php,永远不经过a.phpb.php。您可以通过将第二个参数(称为$replace)设置为false来覆盖此行为:

<?php
header('X-Powered-By: MyFrameWork', false);
header('X-Powered-By: MyFrameWork Plugin', false);
Location头信息只能被指定一次。发送多个Location头将不会将用户重定向到其他页面......这可能会让UA完全糊涂。此外,请注意,在发送Location头之后,代码仍然会继续执行。因此,请在调用header()后紧接着使用exit。这是一个正确的重定向函数:
function redirect($page) {
    header('Location: ' . $page);
    exit;
}

2

试试这样做:

<?php

$page = // use an if statement or whatever you need to figure out 
        //which page you need (pagea.php, etc.)

fnx($page)

function fnx($page) {
    header("location: " . $page);
}

?>

或者

<?php

$page = // use an if statement or whatever you need to figure out 
        //which page you need (pagea.php, etc.)

header("location: " . $page);

?>

2

可能是因为它们并不都在函数中,或者有多个函数被调用了。


2
你遇到这个错误是因为你的脚本已经产生了输出(在调用header()之前使用了echo/print)。你需要在脚本产生任何输出之前调用header()。
来自PHP手册。

http://php.net/manual/en/function.header.php

请记住,在任何实际输出被发送之前必须调用header()函数,无论是通过HTML标签、文件中的空行,还是PHP代码。使用include()、require()函数或其他文件访问函数读取代码时,出现在调用header()函数之前的空格或空行是一个非常常见的错误。同样的问题也存在于单个PHP/HTML文件中。
如果在发送任何输出后尝试调用header()函数,则会收到以下错误...
警告:无法修改头信息-头已经发送。

1

当我需要发送多个头时,我喜欢这样做:

$headers = array(
   'Location' => 'http://www.stackoverflow.com',
   'Content-Type' => ' application/pdf',
);

foreach ($headers as $headerType => $headerValue)
{
   header($headerType . ': ' . $headerValue);
}

使用headers_sent()来检查您是否能够发送头信息。

0

为了那些可能从谷歌搜索而来的人,如果你对于拥有多个相同类型的标题感兴趣:

在PHP中,技术上可以传递多个相同类型的标题。`header`函数有一个名为"replace"的参数。根据文档:

可选的replace参数指示该标题是否应替换先前的类似标题,或添加第二个相同类型的标题。默认情况下,它将替换,但如果你将FALSE作为第二个参数传入,你可以强制使用多个相同类型的标题。

所以你应该能够这样写:

header("location: pagea.php");         #You want to overwrite the initial one, so you'd have this one be default, or true
header("location: pageb.php",false);
header("location: pagec.php",false);

当然,如果您的标头类型不能多次获得,您将会收到错误。在这种情况下,您将收到以下错误(在Chrome中以此方式编写):

此页面无法正常工作

本地主机发送了一个无效的响应。

ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION

对于其他标头,这种方法应该可以正常工作。他们提供的示例是:

header('WWW-Authenticate: Negotiate');
header('WWW-Authenticate: NTLM', false);

0
function one() {
     return "location: pagea.php";
}
function two() {
     return "location: pageb.php";
}
function three() {
     return "location: pagec.php";
}

header(one()); // for example

可能是这样的吗?


0

我有一个不错的解决方案:

die(header("Location: someting.php"));

您可以随时这样做。 (对我来说,这是一个不错的替代方案,因为其他方法都没有起作用)。

特别适用于那些使用多个“header”选项出现问题的人(在我的网站上,无论我怎么做,它始终只会选择一个)。


-1
你可以尝试这样做。
<?php
function one() {
   $redirect=pagea.".".php;

}
function two() {
   $redirect=pageb.".".php;

 }
function three() {
 $redirect=pagec.".".php;
}


header("location:".$redirect);
?>

那甚至都行不通... 当函数结束时,$redirect变量就会超出作用域。 - scott8035

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接