# Solved: php header() command



## snorkytheweasel (May 3, 2006)

I'm trying to do a simple page redirection using PHP

PHP 4.3.8
Apache 1.3.29
(upgrading either or both is not an option)

The code: 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Retrieve Data From BBP Database

<?
header("location: http://www.mydomain.com/reports/report1.php");
?>



Note that there is nothing written to output prior to issuing the command (not even from HTML)

The redirection fails. To confirm , I added some code so it reads as follows:


header("location: http://www.mydomain.com/reports/report1.php");
echo("Error: Using literal for new page");


The redirection does not take place, and my error message displays.

I also tried:


$newpage=" http://www.mydomain.com/reports/report1.php"
header("location: http://www.mydomain.com/reports/report1.php");
echo("Error: Using variable for new page");


The redirection does not take place, and my error message displays.

In the case shown above, I could use javaScript to do the redirection. However, later in the process (on other php pages) I need to pass variables from a form, and use those variables in conditional redirects. javaScript is a nuisance when mixed in with php.

Besides, with PHP I know that I won't have to deal with client-side annoyances like javaScript turned off.

What's happening? How do I do this correctly?

TIA, TTFN


----------



## dudeking (Feb 7, 2007)

I beleive header() needs to be before the tag


----------



## MMJ (Oct 15, 2006)

Dudeking is correct, to elaborate:

A header must be sent to the browser before any output. Why do you output html if you want the page to redirect? If you want to output html before the page redirects you can use a tag.

http://tools.ietf.org/html/rfc2616#section-4.2


----------



## snorkytheweasel (May 3, 2006)

If I understand correctly, a header( ) command, including header("location:"), precedes any HTML, including the tag. That renders the tags, and everything in between, superfluous. If that is so, then a php page with a header( ) command doesn't need anything on that page except 
<?php

header("location:yyy.xxx" );

// plus
// relevant comments 
// and 
// irrelevant comments

?>

On the page that triggered the need for a redirect, I wimped out:

Eliminate the form from which the user makes the selection
Substitute a series of links to the "new" pages
[*]Pretty...ons where the text goes between <A HREF> and 
The simple nature of this circumstance made the <A HREF> solution workable. There is no doubt, however, that in the future a PHP-scripted solution will be in order.

That brings us back to the original problem: using php, how to I perform a conditional redirect?

While on page abc.php,

if user selects (x), then redirect to (or open) page def.php
if user selects , then redirect to (or open) page ghi.php
if user selects (z), then redirect to (or open) page jkl.php


----------



## MMJ (Oct 15, 2006)

> * if user selects (x), then redirect to (or open) page def.php
> * if user selects , then redirect to (or open) page ghi.php
> * if user selects (z), then redirect to (or open) page jkl.php


Using <a> tags would be the most appropriate.


----------

