tags:

views:

263

answers:

6

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?

A: 

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?

inkedmn
+2  A: 

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

Ignacio Vazquez-Abrams
+1  A: 

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

?>
Zack
+1  A: 

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

John Himmelman
+3  A: 

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;
}
Andrew Moore
A: 

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.

Boris Guéry