Toll Free: 1-877-695-7388

GTA: (647) 699-2838

Search Engine People
  • SEO
  • SEM
  • CRO
  • Display
  • Blog
  • Why Us
  • Contact
  • Join Our Team
  • Get A Quote

Toll Free: 1-877-695-7388

GTA: (647) 699-2838

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

Joydeep Deb | February 6th, 2012
Tweet27
Share1
Share
Pin
28 Shares

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.

Error Details

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'] : "http://".$_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.

Tweet27
Share1
Share
Pin
28 Shares
Posted in Web DesignTagged 404, mysql, php

About the Author: Joydeep Deb

Joydeep Deb is a Senior Digital Marketer and Technical Marketer with 12+ years of experience in Digital Marketing, Lead Generation, Online Brand Management, Marketing Campaigns, Search Engine Optimization (SEO), Search Engine Marketing (SEM), PPC, eMail Marketing, Web Analytics, Web Technologies, Web Design and Development.

Blog | Tools | MISC

3 thoughts on “How to use PHP to capture essential Information on 404 Error Page”

  1. Kevin Schroeder says:
    February 6, 2012 at 12:08 pm

    FYI, your code is vulnerable to an SQL injection attack. The user agent and URL are retrieved from the HTTP request and contains user-generated data, which can be used to really muck up your database. Best use prepared statements instead of ad-hoc queries to insert data like this.

    1. Joydeep says:
      February 6, 2012 at 8:50 pm

      Kevin, do share your SQL code with us…

    2. Kevin Schroeder says:
      February 7, 2012 at 7:35 am

      Well, since you’re inserting the user agent you could modify the agent string to be “Whatever my user agent is’; DELETE FROM YOUR_TABLE_NAME;”

Comments are closed.

Recent Posts

  • Maximizing Your E-Commerce Sales:
    A CRO Audit Guide
  • Movin’ On Up! Why Migrating to Google Analytics 4 (GA4) Should be a Priority
  • A Year in Review: The Digital Marketing Trends That Defined 2021
  • The Basics of Video Marketing
  • Just How Much Do Google Reviews Impact Your SEO Ranking?

Categories

  • Analytics & ROI Analysis
  • Company News
  • Content
  • Conversion Optimization
  • CRO
  • Display Advertising/RTB
  • Email Marketing
  • En Español
  • En Français
  • Inbound Marketing
  • Lead Nurture & Marketing Automation
  • Local Search
  • Marketing
  • Mobile
  • Partnership Marketing
  • PPC
  • PR
  • SEO
  • Social Media Marketing
  • Web Design

Additional Posts

Interview with Affiliate Management Days Keynote Speaker Tim Ash

February 3rd, 2012 | by Geno Prussakov

4 Social SEO Mistakes to Avoid

February 3rd, 2012 | by Nick Stamoulis

Turn Your Ads Into An SEO Tool: How Ads Can Affect Organic SERP Ranking

February 3rd, 2012 | by Bradley Zarich

LET'S TALK

Need more information or want to get in touch?

Get in touch!
  • SEO
  • SEM
  • Display
  • Blog
  • Why Us
  • Join Our Team
  • Contact Us
  • Local SEO
  • Small Business SEO
  • Enterprise SEO
  • International SEO

LOCATION

1305 Pickering Parkway,
5th Floor Pickering, L1V 3P2

PHONE

Toll Free: 1-877-695-7388
Greater Toronto Area: (647) 699-2838

Social

© Search Engine People Inc. 2023 – Canada’s Top Digital Agency
© SEP 2023 – A Search Engine People Company | Privacy Policy

Search Engine People