Site icon Search Engine People Blog

How to use PHP to capture essential Information on 404 Error Page

Error Details

Error Details

Stay Connected with Us!

The World Wide Web Consortium (W3C) states that 404 Not Found should be used in cases where the server fails to find the requested location and is unsure of its status. Whenever a page has been permanently removed, the status code used must be 410. But hardly have we seen a 410 page. Instead, 404 Not Found page has become popular and the most commonly used error page.

A custom error page is a placeholder for pages that fail to load when a search engine spider and/or a human visitor tries to view a page. Improperly configured Error Pages may also produce duplicate content issues by producing the same content across unique URLs.

If you have a broken link on your site or the pages, which don't exist any longer, have moved to a new server or have never existed at all, visitors will get an default error message from the server. However, by default these messages aren't that helpful. You can learn here How to setup Custom Error Pages.

What to capture?

Setting up Custom Error Pages isn't enough, if you are not capturing/tracking other information like Page URL, Date/Time, User Agent and Visitor IP etc. You can capture as much information you want. Once you get to know where your website users are hitting 404 or broken links you can easily fix those errors and provide a good user experience.

In my website I capture the Error URL, Response Code, Date/Time, IP Address and User Agent. You can capture any other info also if you think it will be useful for you, let say you have a registered user (signed-in) who have hit a error page, now here you would also like to know who the user was (User Id & Name) and from which page he got the error URL.

How to capture?

I am using PHP to capture these information and storing them in MySQL database, if you wish to you can also send an email to yourself every time if not storing in database (I don't prefer that) or can do both.

Below are the PHP codes I am using it in my Custom Error Page (error-404.html) to capture Error details.

Get Error URL

$get_url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

Get Date Time

$get_date = date("l, F d, Y g:i A T");

Get IP Address

$get_ip = $_SERVER['REMOTE_ADDR'];

Get User Agent

$get_user = $_SERVER['HTTP_USER_AGENT'];

Once you capture these values in PHP variables, you can write a code to insert them in your MySQL Database or Send an eMail to your desired email address.

Store in MySQL Database

$con = mysql_connect("localhost","LOGIN_ID","PASSWORD");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("YOUR_DB", $con);
mysql_query("INSERT INTO YOUR_TABLE_NAME (error_type, ip_address, date_time, user_agent, error_url)
VALUES ('Page Not Found - 404 Error','$get_ip','$get_date','$get_user','$get_url')");
mysql_close($con);

Testing the Error Setups

When you're satisfied with your 404 error page, upload it together with your .htaccess file to your website. Then test it by typing a URL that you know does not exist. Your error page should load up and insert the error details into your database.

Thanks to PHP and .htaccess, I am able to trace all the Errors users are hitting on my website, hacking attempts and broken links and fix those issues ASAP. Please do share what other information you would like to capture from your Custom 404 Error Page.