Counting Views of a Single Web Page
This question was received in an email a few days ago.
"Say you have a special page you put up and need to track how many times it is viewed. How would you do that for just one page?"
A script can append a line to a log file every time a page is loaded. The number of lines in the log file is the number of page views.
Yet, consider:
-
Spiders may cause a line to be appended to the log file. Those would then be counted as views.
-
A web page may be reloaded or revisited. Are reloads and revisits to be counted as views?
The software I made in response to this question:
-
Uses JavaScript to log page views. Most spiders do not follow JavaScript.
-
Can be told to ignore reloads/revisits when it displays statistics.
Know how often a special page is viewed. The software to record views and provide the total is PHP.
In the places marked, specify a password (or no password to remove password protection) and the location of the log file (which must exist and might need 777 permissions).
Then upload it to your server. In this article, we'll assume you named it viewcount.php, but it can have any legal PHP file name.
<?php // First line of PHP file (no blank lines above) /* Counting Views of a Single Web Page Version 1.1 September 4, 2012 (Version 1.0 published February 7, 2011 Will Bontrager https://www.willmaster.com/ Copyright 2011 Bontrager Connection, LLC Copyright 2012 Will Bontrager Software, LLC This software is provided "AS IS," without any warranty of any kind, without even any implied warranty such as merchantability or fitness for a particular purpose. Will Bontrager Software, LLC grants you a royalty free license to use or modify this software provided this notice appears on all copies. */ /* 1. Do customization (below) and upload this script to server. 2. To log page views, call this script as an external JavaScript file. 3. To get stats, type URL of this script with "?stats" appended. */ /* C u s t o m i z a t i o n */ // Specify a password for viewing the statistics. // If password contains ", escape it like: \" // (To remove password protection, leave password blank.) $StatisticsPassword = "mypass"; /* Here is a nice password generator: https://www.willmaster.com/library/generators/Password_Generator.php */ // Specify the location of the log file. // Directory needs to exist and may need 777 permissions. $LogFileLocation = "/subdirectory/log.txt"; /* End of customization */ $tm = time(); $Ignore = true; $GetLogin = false; $Stats = $_SERVER['QUERY_STRING'] == 'stats' ? true : false; if( $Stats ) { if( isset($_POST['TVoaSWP']) ) { $goahead = empty($StatisticsPassword) ? true : false; if( (!$goahead) and $_POST['pw'] == $StatisticsPassword ) { $goahead = true; } if( $goahead ) { $ignoredups = (isset($_POST['dups']) and $_POST['dups'] == 'ignore') ? 'yes' : 'no'; setcookie('TVoaSWP',$ignoredups,$tm+3); header('Location: ' . $_SERVER['PHP_SELF'] . '?stats'); exit; } else { $GetLogin = true; } } elseif( empty($_COOKIE['TVoaSWP']) ) { $GetLogin = true; } else { $Ignore = $_COOKIE['TVoaSWP'] == 'yes' ? true : false; } } else { $f = fopen($_SERVER['DOCUMENT_ROOT'].$LogFileLocation,'a'); if( $f ) { if( fwrite($f,date('H:i:s',$tm).','.date('d M Y',$tm).','.$_SERVER['REMOTE_ADDR'].",$tm\n") ) { header("Content-type: text/javascript"); echo "var dskksdskdfksdsss"; } else { echo "alert('Unable to append to log file $LogFileLocation');"; } fclose($f); } else { echo "alert('Unable to open log file $LogFileLocation for appending.');"; } exit; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" > <title> </title> <style type="text/css"> body { margin:0; font-family:sans-serif; } th { font-size:12px; } p, li, td { font-size:14px; line-height:120%; } a { text-decoration:none; border:none; outline:none; } img { border:none; outline:none; } #content { margin:0 0 0 150px; padding:75px 0 100px 50px; width:550px; border-left:6px groove blue; } #block_center { margin:0 auto 0 auto; text-align:left; width:200px; border:1px solid blue; } </style> </head> <body><div id="content"> <h3> <a href="//www.willmaster.com/"><img src="//www.willmaster.com/images/wmlogo_icon.gif" width="50" height="50" border="0" alt="Willmaster logo" title="Willmaster Software"></a> Counting Views of a Single Web Page </h3> <div style="height:50px;"> </div> <?php if( $GetLogin ): ?> <form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>?stats"> <input type="hidden" name="TVoaSWP" value="1'> <p> <input type="checkbox" name="dups" value="ignore" checked="checked"> Ignore duplicate IP addresses. </p> <?php if( ! empty($StatisticsPassword) ) { echo <<<FIELD <p> Password:<br> <input type="password" name="pw" style="width:300px;"> </p> FIELD; } ?> <p> <input type="submit" value="Get Number of Views" style="width:300px;"> </p> </form> <?php else: ?> <h4>Views</h4> <?php $buf = ''; $IP = array(); $f = fopen($_SERVER['DOCUMENT_ROOT'].$LogFileLocation,'r'); if( $f ) { while( ($buf = fgets($f,1024)) !== false ) { #$buf = rtrim($buf); $ta = explode(',',$buf); if( $Ignore ) { $IP[$ta[2]] = 1; } else { @$IP[$ta[2]] += 1; } } if( ! feof($f) ) { echo '<h5>Unexpected error while reading log file</h5>'; } fclose($f); } $numview = 0; foreach( $IP as $k => $v ) { $numview += $v; } $IP = array(); ?> <p> Number of views: <b><?php echo($numview); ?></b> </p> <?php endif; ?> <p style="margin-top:50px;"> Copyright 2011 <a href="//www.willmaster.com/">Bontrager Connection, LLC</a><br> Copyright 2012 <a href="//www.willmaster.com/">Will Bontrager Software, LLC</a> </p> </div> </body> </html>
Make a note of the URL of viewcount.php
Here is the JavaScript to log each page view. Replace http://example.com/viewcount.php with the URL of viewcount.php
<script type="text/javascript" src="https://example.com/viewcount.php"></script>
Put the JavaScript into the web page to log.
To get the page view count, type the URL of viewcount.php into your browser with "?stats" appended. Example: http://example.com/viewcount.php?stats
If a password has been specified in viewcount.php source code, the control panel will ask for the password.
Check or uncheck the box to ignore duplicate IP addresses. Then click the "Get Number of Views" button.
The next page displays the page views total.
The system will log each view of a specific web page. The software's control panel is used to get the page view count.
Will Bontrager