开发者

How to declare more than one header on PHP

开发者 https://www.devze.com 2022-12-22 12:24 出处:网络
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:

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("locat开发者_如何学Goion: 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?


I think you misunderstand what the HTTP header Location does.

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

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

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

<?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.php or b.php. You can override this behavior by passing a false value to the second parameter (called $replace):

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

The Location header can only be specified once. Sending multiple Location header 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 Location header. So follow that call to header() with an exit. Here is a proper redirect function:

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


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

?>


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


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.

From the PHP Manual

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.

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

Warning: Cannot modify header information - headers already sent


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.


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?


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


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

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

The optional replace parameter 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 FALSE as the second argument you can force multiple headers of the same type.

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

This page isn’t working

localhost sent an invalid response.

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消