note 53751 added to function.header
| From: | cameron at prolifique dot com | Date: | Sat, 11 Jun 2005 19:29:42 +0000 |
| Subject: | note 53751 added to function.header | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90273@lists.php.net to get a copy of this message | ||
I use a custom PHP 404 page that does several things:
Checks the database for a "virtual" page matching the missing page address, and if
found, includes and displays it, issuing an HTTP 200 OK status. The client browser receives the page
contents and never even knows that the page doesn't actually exist on the server.
Checks the database for a redirect from this missing page address to another address, and if
found, does the redirect by issuing an HTTP 302 Object Moved status and Location header.
Tries to fix common address errors, such as removing periods or extra slashes at the end of the
address, and then checks for a physical page with a matching address, a virtual page with a matching
address, or a redirect, and takes the appropriate action.
If all of the above fail, it simply shows a friendly "Page Not Found" error.
In the case of redirects and genuine missing pages, the missing page address (and the number of
hits on it) is logged to the database. In the case of genuine missing pages, an email is also sent
to me so I can identify and fix broken links.
The "virtual" page behavior is perhaps the most useful, as it allows me to create totally
dynamic pages on the server but use static addresses for them.
However, on one particular server on which I used this system, my HTTP 200 header wasn't
getting sent correctly:
<?php
header("HTTP/1.1 200 OK");
?>
Instead, the browser received a 404, and even if the virtual page content was included and sent
correctly (it was), IE showed its typical 404 error page instead based on the status code (but only
if "show friendly HTTP error messages" is checked, which is the default).
Finally found this note in an older or alternate version of the PHP manual (at http://academ.hvcc.edu/~kantopet/php/manual/function.header.html):
Note: In PHP 3, this only works when PHP is compiled as an Apache module. You can achieve the same
effect using the Status header.
<?php
header("Status: 404 Not Found");
?>
Though our server uses PHP/4.3.10, I thought this was worth a try, so I tried issuing both commands:
<?php
header("HTTP/1.1 200 OK");
header("Status: 200 OK");
?>
And, in fact, it worked.
So if you find that using header("HTTP/1.1 ###") isn't working, try the
header("Status: ###") instead or as well.
----
Manual Page -- http://www.php.net/manual/en/function.header.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53751
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53751&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53751&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53751&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53751&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53751&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53751&report=yes
Search -- http://master.php.net/manage/user-notes.php