如何在 PHP 上声明多个标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2427878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:26:29  来源:igfitidea点击:

How to declare more than one header on PHP

phpheader

提问by Homer Simpson

I want to send my users to different pages based on user action. So I made multiple functions at the top of the page like so:

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

<?php

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

?>

Of course I get an error because I am re declaring headers. At first I though it was going to be okay since I am containing them inside functions and am calling any one function at a time. But still I get the error. Is there any other way of doing this?

当然,我收到一个错误,因为我正在重新声明标题。起初我认为这会没事,因为我将它们包含在函数中并且一次调用任何一个函数。但我仍然收到错误。有没有其他方法可以做到这一点?

回答by Andrew Moore

I think you misunderstand what the HTTP header Locationdoes.

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

The Locationheader instructs the client to navigate to another page. You cannot send more the one Locationheader per page.

Location头指示客户端导航到另一个页面。Location每页不能发送更多一个标题。

Also, PHP sends headers right before the first output. Once you output, you cannot specify any more headers (unless you are using Output Buffering).

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

If you specify the same header twice, by default, header()will replace the previous value with the latest one... For example:

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

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

will redirect the user to c.php, never once passing by a.phpor b.php. You can override this behavior by passing a falsevalue to the second parameter (called $replace):

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

<?php
header('X-Powered-By: MyFrameWork', false);
header('X-Powered-By: MyFrameWork Plugin', false);

The Locationheader can only be specified once. Sending multiple Locationheader will not redirect the users to the pages... It will probably confuse the crap out of the UA. Also, understand that the code continues to execute after sending a Locationheader. So follow that call to header()with an exit. Here is a proper redirect function:

Location标头仅可指定一次。发送多个Location标头不会将用户重定向到页面......它可能会混淆 UA 的废话。另外,请理解代码在发送Location标头后继续执行。因此,请header()使用exit. 这是一个正确的重定向函数:

function redirect($page) {
    header('Location: ' . $page);
    exit;
}

回答by Zack Marrapese

Try something like this:

尝试这样的事情:

<?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);
}

?>

or

或者

<?php

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

header("location: " . $page);

?>

回答by Ignacio Vazquez-Abrams

Either they aren't all in functions or more than one is being called.

要么它们不是全部在函数中,要么被调用的不止一个。

回答by John Himmelman

You're getting that error because your script has already produced output (you used echo/print before calling header()). You need to call header() before your script produces any output.

您收到该错误是因为您的脚本已经生成了输出(您在调用 header() 之前使用了 echo/print)。您需要在脚本产生任何输出之前调用 header()。

From the PHP Manual

来自 PHP 手册

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

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

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include(), or require(), functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

请记住,header() 必须在发送任何实际输出之前调用,无论是通过普通的 HTML 标记、文件中的空行还是来自 PHP。使用 include() 或 require() 函数或其他文件访问函数读取代码并且在调用 header() 之前输出空格或空行是一个非常常见的错误。使用单个 PHP/HTML 文件时存在同样的问题。

You'll get the following error when you attempt to call header() after sending any output...

当您在发送任何输出后尝试调用 header() 时,您将收到以下错误...

Warning: Cannot modify header information - headers already sent

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

回答by Boris Guéry

Here is how i like to do when i need to send multiple headers :

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

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

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

Use headers_sent()to check if you'll be able to send headers or not.

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

回答by Mohsin Younas

You can try something like that.

你可以尝试这样的事情。

<?php
function one() {
   $redirect=pagea.".".php;

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

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


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

回答by Josh Powlison

For the sake of people who may be coming here from Google, if you're interested in having multiple of the same typeof header:

为了那些可能从 Google 来到这里的人,如果您有兴趣拥有多个相同类型的标题:

It is technically possible to have multiple headers that are of the same type passed in PHP. headerhas a parameter called "replace". From the documentation:

从技术上讲,可以在 PHP 中传递多个相同类型的标头。header有一个名为“replace”的参数。从文档:

The optional replaceparameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in FALSEas the second argument you can force multiple headers of the same type.

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

So you should be able to write:

所以你应该能够写:

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);

Of course, if your type of header can't be had multiple times, you'll get an error. In this case you get this error (written this way in Chrome):

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

This page isn't working

localhost sent an invalid response.

ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION

此页面无效

localhost 发送了无效响应。

ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION

For other headers, however, this method should work fine. The example they supply is:

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

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

回答by Insencel

I have a nice solution:

我有一个很好的解决方案:

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

You can do that as often as you want. (Nothing else worked for me and to me this is a nice alternative)

您可以根据需要经常这样做。(没有其他方法对我有用,对我来说这是一个不错的选择)

Especially for everyone who has a problem with using multiple options for "header" (on my site it would always just take one, no matter what I did)

特别是对于对“标题”使用多个选项有问题的每个人(在我的网站上,无论我做什么,它总是只需要一个)

回答by inkedmn

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

header(one()); // for example

Maybe something like that?

也许是这样的?