   [web] PHP Header
  I've been learning php recently and have used the header() function to redirect.In the php manual (http://php.net/manual/en/function.header.php) it states that"must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP."For example:<html><?php/* This will give an error. Note the output * above, which is before the header() call */header('Location: http://www.example.com/');?>I'm finding that this is not the case, and I am not getting an error.I want to know why it works (or does it work for anyone else), or if I am somehow misunderstanding the functions use.I'm using Apache server 2.2 and PHP 5.2.12Thanks   
Quote:Original post by nathan123...must be called before any actual output is sent......I'm finding that this is not the case, and I am not getting an error...Because you don't get an error doesn't mean it isn't true. Header information must be send first, ever, without exception. Thats how the underlying protocol (HTTP) works.Now if it works for YOU means that there is something between your file and your servers output stream. If your server configuration buffers the output (e.g. look for output_buffering in PHP's configuration file php.ini) than the header can be send before any output of your file, even if PHP's header function gets called at the end of your PHP file.   ;  Check to see if output buffering is enabled in your php.ini.  The variable to look for is output_buffering.See Output Control in the PHP manual for more info:http://php.net/manual/en/book.outcontrol.php   ;  Thanks for the responses.  I'm still learning the ins and outs and thought I misunderstood something.  I appreciate your help!   ;  Output buffering is an advanced topic, although admittedly fun to play with. However, unless you are writing the most trivial code, i would recommend you write your code in such way to output everything all at once at the end of whatever you're doing. Something like:<?phpDoStuph();DoMoreStuph();OutputEverything();Cleanup();?>That way, you can issue a header or redirect earlier in the code and not have to worry about prior output, for example. Templates usually solve this issue by themselves. Seeing output outside of <?php> tags just makes me cringe.   ;  Or simply use PHP's output buffering. Look into ob_start() and ob_end_flush(). That way the code will even work when Apache output buffering is off.   