note 85254 added to function.header

From: Date: Fri, 22 Aug 2008 06:57:01 +0000
Subject: note 85254 added to function.header
Groups: php.notes 
Request: Send a blank email to php-notes+get-143736@lists.php.net to get a copy of this message
It is important to note that headers are actually sent when the first byte is output to the browser. If you are replacing headers in your scripts, this means that the placement of echo/print statements and output buffers may actually impact which headers are sent. In the case of redirects, if you forget to terminate your script after sending the header, adding a buffer or sending a character may change which page your users are sent to. This redirects to 2.html since the second header replaces the first. <?php header("location: 1.html"); header("location: 2.html"); //replaces 1.html ?> This redirects to 1.html since the header is sent as soon as the echo happens. You also won't see any "headers already sent" errors because the browser follows the redirect before it can display the error. <?php header("location: 1.html"); echo "send data"; header("location: 2.html"); //1.html already sent ?> Wrapping the previous example in an output buffer actually changes the behavior of the script! This is because headers aren't sent until the output buffer is flushed. <?php ob_start(); header("location: 1.html"); echo "send data"; header("location: 2.html"); //replaces 1.html ob_end_flush(); //now the headers are sent ?> ---- Server IP: 208.69.120.35 Probable Submitter: 71.98.139.133 ---- Manual Page -- http://www.php.net/manual/en/function.header.php Edit -- https://master.php.net/note/edit/85254 Del: integrated -- https://master.php.net/note/delete/85254/integrated Del: useless -- https://master.php.net/note/delete/85254/useless Del: bad code -- https://master.php.net/note/delete/85254/bad+code Del: spam -- https://master.php.net/note/delete/85254/spam Del: non-english -- https://master.php.net/note/delete/85254/non-english Del: in docs -- https://master.php.net/note/delete/85254/in+docs Del: other reasons-- https://master.php.net/note/delete/85254 Reject -- https://master.php.net/note/reject/85254 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143736) next »