Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibrarySnooping (Information Retrieval)

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Page Load Log and Email Alert

When a site owner would like to know whether or not a specific web page is being viewed, a log can be maintained and/or an email sent if the page is accessed.

A short PHP script to do that comes with this article.

An actively updated website tends to accumulate pages that aren't linked from anywhere within the site. Yet, they may be linked to from a remote domain. Those pages can be updated with the short PHP script to monitor access.

Another use is to monitor access to semi-private pages provided for specific people.

A third reason is to be informed whenever a download page or purchase thank-you page is loaded into a browser.

If you already have adequate statistics software, it may be used to see if specific pages were accessed. Or, you can scan the server access logs.

The short PHP script provided below is probably an easier way. Or at least simpler. It monitors only the page the short PHP script is on, not the entire site.

Page access information includes IP address, user-agent data, and referrer URL if available:

  • The IP address can be used to see if the same browser visited more than once.

  • The user-agent data can be helpful to determine whether or not the page was accessed with a bot or spider.

  • The referrer URL can indicate where incoming links are located.

The short PHP script is customized with the log file location and an email address. Both of those are optional in case you want only one or the other.

Here is the PHP script. Customization notes follow.

<?php
$EmailAddress = "";
$LogFileLocation = "";
date_default_timezone_set("UTC");
$referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$data = array(date('r'),$_SERVER['PHP_SELF'],$_SERVER['REMOTE_ADDR'],$_SERVER['HTTP_USER_AGENT'],$referrer);
if( strlen($LogFileLocation) )
{
    $file = "{$_SERVER['DOCUMENT_ROOT']}$LogFileLocation";
    file_put_contents($file,implode("\t",$data)."\n",FILE_APPEND);
}
if(strpos($EmailAddress,'@')>0) { mail($EmailAddress,"Report from {$_SERVER['PHP_SELF']}",implode("\n",$data),"From: $EmailAddress"); }
?>

Customization —

The first two lines of PHP code must be customized for email address or log file location. The third line may be customized for a timezone change.

  1. At the first line of the PHP code, between the quotation marks near the end of the email address line, colored blue, is where you specify the destination email address — if you want to receive an email whenever the web page is accessed.

    When no email address is specified, no email is sent.

  2. At the second line, between the quotation marks near the end of the log file location line, colored red, is where you specify the location of the log file — if you want a log file to be updated whenever the web page is accessed.

    The location of the log file is the URL of the file minus the leading protocol and domain name. As an example, the URL https://example.com/data/log.txt for URL of the log file would be /data/log.txt for the location of the file.

    When no log file location is specified, no logging is done.

  3. At the third line of the PHP code, you'll see:

    date_default_timezone_set("UTC");

    The UTC is the timezone designation. (In this case, Universal Time, same clock as Greenwich Mean Time.) The timezone designation affects the time reported for the web page access time in the email and log file.

    To change the timezone, use Timezone Designations for PHP Software to determine the timezone you want. Then, in the above PHP script, replace UTC with the timezone designation of your choice.

When the customization is done, the PHP script can be inserted into PHP web pages you want to monitor.

Specifying the same log file location for each web page with the PHP script will give you one central log file. Specifying a unique log file location will give you a unique log file for that page.

Test the update, of course, for each web page that has the PHP script inserted. Verify you get the email and/or the log file is updated according to your preferences.

  • Debugging email: See the Debugging Email Sent With a PHP Script blog post for things that can be addressed to get email sent and received.

  • Debugging file writes: The directory where the log file is to be written, or the file itself, may need 777 permissions before the file can be created or updated.

With the PHP script on the web page, whenever the page is accessed, a note is made of the event.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC