note 56324 added to function.header

From: Date: Tue, 30 Aug 2005 17:57:58 +0000
Subject: note 56324 added to function.header
Groups: php.notes 
Request: Send a blank email to php-notes+get-94440@lists.php.net to get a copy of this message
It seems as though the "Location" header in PHP has a special affinity for 3xx level HTTP response codes. Using Linux/Apache and PHP 4.3.9, the following two bits of code will behave exactly alike: <?php header("HTTP/1.1 307 Temporary Redirect"); header("Location: http://www.example.com/error.php"); ?> <?php header("Location: http://www.example.com/error.php"); header("HTTP/1.1 307 Temporary Redirect"); ?> ... redirecting to /error.php with a "307 Temporary Redirect" response code. Other 3xx and 2xx level responses will behave in a similar fashion. However, using other response codes (4xx and 5xx level) causes the output to differ depending on order. <?php header("HTTP/1.1 412 Precondition Failed"); header("Location: http://www.example.com/error.php"); ?> ... will redirect to /error.php with an HTTP response code of "302 Found" while: <?php header("Location: http://www.example.com/error.php"); header("HTTP/1.1 412 Precondition Failed"); ?> ... does the same, but this time sends the "412 Precondition Failed" header as specified. I would make a guess that in the first case, PHP recieves the "Location" header, and upon noting that a 3xx or 2xx level response has not been set (even though a 4xx or 5xx response *has*), sets the "302 Found" to create a "proper" HTTP redirect. In the latter case, PHP sets the "302 Found" header, but it is overwritten by the new response code which appears later in the code. While sending a "Location" header with 4xx and 5xx level responses is unconventional, the spec does not prohibit doing so: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30 ---- Manual Page -- http://www.php.net/manual/en/function.header.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56324 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56324&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56324&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56324&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56324&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56324&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56324&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56324&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94440) next »