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.
-
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.
-
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.
-
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