Re: newbie question about header()

From: Date: Mon, 22 Dec 2003 05:18:28 +0000
Subject: Re: newbie question about header()
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-173126@lists.php.net to get a copy of this message
On Monday, December 22, 2003, at 01:13 PM, Website Managers.net wrote:
Unless you're using an 'if' statement, the header redirect must be the first line of the page, above any HTML markup.
That's not entirely accurate. ---Quoted from http://php.net/header --- 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() doesn't need to be 'the first line of the page', but it does need to be placed above/before any output gets sent to the browser. bad: <html> <? header(...); ?> bad: <? echo "foo"; header(...); ?> good: <? include('something.inc'); session_start(); $foo = 'bah'; header(...); ?> if there's any code following the redirect (which you don't want to execute), I'd also strongly recommend having an exit; after the header(): <? include('something.inc'); if($foo) { header(...); exit; } // some other stuff that shouldn't get executed after redirect ?> Justin French

« previous php.general (#173126) next »