php
header function
In getting this site, jayjohnston.com, about php setup, I created a google sitemap in webmaster tools. Interestingly, I discovered that google was caching some pages that were from the previous jayjohnston.com owner. the pages were of no account and not ranked in a way that is helpful to me.
The casual reader may be thinking, "if they are no longer in your site framework, why worry?" Well, I write in a framework the great php engineer, Will Fitch originally termed "PHP-MVC". He has since stopped using it and refuses to to claim it since I took over its development. Nevertheless, in my heart it will always be his, no matter how much I have made it my own.
So, now that we've coined the term "PHP-MVC", let me point out that php-mvc doesn't send 404's. If a file system resource is unavailable, rather than 404 error the request, php-mvc sends the default page of the site.
Google still thought the previous owner's pages were on my site and it gigged me for "Duplicate title tags" on the two pages.
Send a 404 error header!
<?php
header('HTTP/1.0 404 Not Found');
?>
Now, that is the example code given in the php manual under 'header' function. However it is best practice (and was required in my situation) to cease further processing afterwards. Use the exit/die function.
header('HTTP/1.0 404 Not Found');
die();
or
header('HTTP/1.0 404 Not Found');
exit();
Send a 302 redirection header!
In the case that you would like to redirect a user, you can use the same header function with a slightly different function parameter:
// choose to redirect,
$url = '/local/page.htm'; // internally or
$url = 'http://jayjohnston.com'; // externally,
header('Location: '.$url); // redirect, and then
exit(); // stop processing!
Cannot modify header?
If you are getting an error like this:
Warning: Cannot modify header information - headers already sent by (output started at
...
then you need to be sure you are not sending any output (not even a single space character), before sending the header.
Last Updated: 2008-08-30 11:02:12