Custom 404 Error Page in PHP

by tridecagon in Circuits > Websites

59717 Views, 11 Favorites, 0 Comments

Custom 404 Error Page in PHP

404_one.png
404_two.png
Every website gets the occasional, frustrating Error 404: Not Found. And if you have your own website, you may wish to customize these error pages. Thankfully, it's not that hard to do. Your error pages may be of any extension you want. Usually, they are written in SHTML. But SHTML isn't very dynamic in terms of what can be done with it. So I went over to PHP for my error pages. The coding wasn't hard either. So let's begin.

Requirements

If you have a website, your hosing server should have PHP installed. If not, ask your server's admin if they would be kind enough to install it. If you are just screwing around wasting time, you need some type of emulator. If you're on Windows, use easyPHP for this. If you're on Linux and can spare the resources, get: apache2 and php (for Ubuntu, sudo apt-get install apache2 php). If you're on mac, I have no idea what you can use.

Once you meet these requirements, go to the next step.

.htaccess

htaccess.png
As stated on the cover page, your custom error page(s) can have any extension you want. But you can't use it if your server isn't told to use it. This is where .htaccess comes in. .htaccess is a file named, well, ".htaccess". This file can be used to configure your site to certain degrees. What we will do is go ahead and point the 404 error to 404.php (which, ironically, doesn't exist yet). This will be a hidden file (every file that begins with a dot is a hidden file). So make sure you can view hidden files. In this file, write the following code and save it:
ErrorDocument 404 /404.php
And while you're at it, if you feel like it:
ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php #Don't re-add this line
ErrorDocument 500 /500.php

404.php

404_php.png
And the actual error page. I'm going to ignore the whole "let's follow standards" thing I usually do. The reason I chose PHP for the error page is because you can figure out the source of the error to an extent; you can see if the missing page was typed in the address bar, if it was a link on your site, or if it was a link on a different site. This is achieved by parsing a server variable. You can also parse a couple other server variables and see exactly what was put into the address bar. For this ible, we will only grab the requested page and the referrer, if any.

<?php echo $_SERVER['REQUEST_URI']; ?> does not exist, sorry.

The line above will tell the visitor that the page they want, along with the page's path (preceded with a slash), does not exist. It's helpful to tell the specific page because the hyperlink they followed, if they followed, may not reflect the page's path. The next code will grab if there was a referrer and who it was.

<?php
if(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])){
$refuri = parse_url($_SERVER['HTTP_REFERER']); // use the parse_url() function to create an array containing information about the domain if($refuri['host'] == "your-domain.com"){
//the link was on your site
}
else{ //the link was on another site. $refuri['host'] will return what that site is
} } else{
//the visitor typed gibberish into the address bar
}
?>

On my site, I told the user one of three things to do as per the code. If the referrer was my site, email me and let me know. If the referrer was on a different site, email them and let them know. If they types randomly in the address bar, stop doing that.

<?php
if(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])){
$refuri = parse_url($_SERVER['HTTP_REFERER']); // use the parse_url() function to create an array containing information about the domain
if($refuri['host'] == "cutlery-in-the-toaster.com"){
echo "You should email fork@cutlery-in-the-toaster.com and tell me I have a dead link on this site.";
}
else{
echo "You should email someone over at " . $refuri['host'] . " and let them know they have a dead link to this site.";
}
}
else{
echo "If you got here from Angola, you took a wrong turn at Catumbela. And if you got here by typing randomly in the address bar, stop doing that. You're filling my error logs with unnecessary junk.";
}
?>

Testing

First, go to your site like normal. It should show up normally. To test if your .htaccess is being read, insert random junk anywhere inside it and save it. Reloading the page should give a 500 error. If not, make sure your site is set up to use .htaccess files (I just had to edit my server's config files to get it to work). If it still don't work, try deleting all the blank spaces and reinserting them.
Else, undo the junk and re-save it. Now try to visit a non-existent page. You should see your 404 page. Add a few dead links on your site and try to follow them. You should end up with the same 404 page but with different content. Add a dead link to another website and the 404 page will have different content.

Files

Here are the files used in this ible. Edit as desired/required.

Downloads