Building Web Projects

with Server-Side Includes, a little Perl, and some JavaScript
Get Notified When Someone Hits Your Webpage

Get Notified When Someone Hits Your Webpage

Published: August 30, 2024
Revised: September 12, 2024
Post a comment

Overview

I've come across a situation where I really wanted to know if one of my webpages is accessed. The actual case came up when I notified a company about some problems with their website. Rather that send a large email with images to show the problem, I just built a webpage. On that page I explained how I discovered the problem and then provided lots of screenshots to show the problem. Then I sent an email to their contact explaining the problem and pointing them to the webpage, and waited for a response. Nothing came. I at least expected a reply saying they would look into the problem but instead there was silence. I then decided to check the access logs, which I was already doing for other reasons, but saw that no one had accessed the webpage I had put togther.

Having done web development for years, I had to wonder why apparently no one was monitoring the email address that was posted on the site, and why no one seemed to care about the condition of the site. Frustrating, yes, because I always appreciated feedback from visitors so I could fix or improve things. Well, I guess when you're a big company who's outsourced their external website (and I know this for a fact) you're not responsible to actual visitors, just the company who paid for your services, and they are unaware.

As you can imagine, checking access logs takes time. Then I had this idea. I already have code that notifies me when a 404 error in encountered on one of my site (See my arrticle, "A More Useful 404)," why not take advantage of that? And that is exactly what I did.

Solution

Since I wanted to use the code I wrote for sending emails to me when one of my webpages generated a 404 error, all I had to do was intentionally generate a 404 on the page of interest. So, to do this I created a link to a non-existing image. And so a broken image would not appear on the webpage itself, I added some CSS as well. Below is this code.

<img src="blank.gif" style="visibility: hidden;">

I could put this anywhere on the webpage, but I chose to put it at the bottom. And if I wanted this on all of the pages on a site I only need to included it in the footer section which I then use Server-Side Includes (SSI) to import.

When the webpage is accessed, the missing image will generate a 404 error and my 404 code, detailed in "A More Useful 404" will send me an email. To read how I use SSI and Perl to process 404 errors, and to see the entire 404.pl script, just check out "A More Useful 404."

When my webpage is accessed I receive the following email. (Yes, the hostname and IP below are fictitious.)

From: 404Script@www.buildingwebprojects.com

Subject: Someone at the.host.name (1.2.3.4) has just visited your page, http://www.buildingwebprojects.com/articles/get-notified-when-someone-hits-your-webpage/

Message: Your webpage, http://www.buildingwebprojects.com/articles/get-notified-when-someone-hits-your-webpage/, was just visited by the.host.name (1.2.3.4) and the non-existing file, 'blank.gif,' generated this 404 error.

I hope you find this useful.