Born out of boredom, unrealized necessity and the fruit of another script, this really simple PHP counter logs page views and tracks visitors from the time they enter your site to the time they leave it. Weighing in at slight 9.9 KiB, or 5.6 KiB when all comments are removed, it represents excellent value for a free php website traffic counter and logger.
The traffic data logged by Really Simple Traffic Logger is everything you might need to see where your visitors are coming from, where they are going once they are on your site and for optionally displaying a configurable PHP counter on your webpage. The complete log data list is:
- Access date
- Access time
- Visitor’s IP address
- Visitor’s hostname
- The visitor’s referrer (where he/she or it came from)
- The visitor’s browser agent
- The page (or pages) the visitor viewed (or loaded)
- The script that loaded an accessed page
- Any search engine search strings
- Any search engine keywords
If a visitor’s browser agent does not provide any loggable information then Really Simple Traffic Logger, or STL, as it’s affectionately known, still records the access time. This means that all page loads are recorded.
Note that Really Simple Traffic Logger does not differentiate between bots and real visitors. It logs everything. This means that any visible counter will make your site look busier than it actually is. If you want to fudge the data a little, you can easily add to your counter’s starting figure.
There are two modes of operation: cycle and session.
Session mode tracks visitors during a set time frame within one 24 hour period only. This is useful when you know exactly when you need to monitor traffic and you wish to keep your traffic log file to that specific time frame only.
Cycle mode tracks visitors continuously or until the script is disabled. Cycle mode can be configured to monitor traffic during a set period of each 24 hour cycle. This is the mode you are most likely to use.
Instructions
To use STL to monitor your website’s traffic:
- download Really Simple Traffic Logger
- upload it to your server
- unzip it
- rename the directory to stats.
- configure the file stl.php before including it in the footer of your website.
<?php include("stats/stl.php"); ?>
The unzipped file is a directory called “Really_Simple_Traffic_Logger”. Change that directory name to just “stats”. The traffic logging script is called stl.php. There is a subdirectory under ‘stats’ called ‘data’. The data directory contains all the stats logs made by STL. You can browse the directory by going to http://example.com/stats/data/
Configure the script by opening it for editing and change the settings under the headline CONFIGURABLE VARIABLES.
WordPress Integration
WordPress integration is easy. No theme editing required… in most cases. Use the WordPress plugin Ultimate TinyMCE. This plugin has an option to put a PHP widget under Appearance > Widgets
Configure the traffic logging script before adding it into your site.
If you unpack the zip file that contains the PHP script into your WordPress site’s installation folder, you would configure the variable $dpath (in stl.php) to be:
$dpath = "stats/"
This is the default location for $dpath. If you unzip Really Simple Traffic Logger package to any other location then you must change $dpath to reflect that location.
Once Ultimate TinyMCE is installed and configured to give you a PHP widget, you can include stl.php in the widget with
<?php include (stats/stl.php); ?>
The Output Files
Really Simple Traffic Logger creates four output file in its data directory:
- log.csv
- se_archive[date].csv
- cy_archive[date].csv
- tally.txt
log.csv stores all log data per session and cycle. It is updated whenever a page is viewed. It is wiped clean whenever an archive file is written. Except the header, each line of data stored in log.csv represents a page view.
se_archive[date].csv stores the data from single session logs (log.csv) just before log.csv is wiped. Only one of these is written per date. Clear these out once you have finished with them or you will end up with one for as many days as STL runs.
cy_archive[date].csv stores the data from cycle session logs (log.csv) just before log.csv is wiped. Only one of these is written per date. As with se_archive[date].csv files, clear these regularly.
tally.txt stores the count of lines in every created log.csv file just prior to log.csv being wiped.
log.csv, se_archive[date].csv and cy_archive[date].csv store data in standard csv format and may be read by most spreadsheet programs.
If you need to create multiple archives per day then you need to change either the value of “se_archive” in $archive=”se_archive” or “cy_archive” in $archive=”cy_archive” or you need to remove the existing archive log for that day first.
Fudging the Data
If you want to set a starting figure for your counter’s page hits record, add the figure’s number to the top of the tally.txt file. If the file does not yet exist, create it.
Fudging the data is handy when migrating from other PHP counters or other methods of counting site visitor stats.
Using the Log Data
Open the log file you wish to view in a spreadsheet application like LibreOffice Calc or MS Excel as a comma separated file. Each log file has a header that looks like this:
"DATE","TIME","IP","HOSTNAME","REFERRER","AGENT","PAGE","DOC_ROOT","SCRIPT","PATH","LOOKING_FOR","KEYWORDS"
Each comma separated item represents a data column. Visitor data is stored in a separate row per page viewed and per page reloaded. Separate components of the tracked information are stored in individual columns. The information stored in each column is:
- Date
The date of the visit - Time
The time of the visit - IP
The visitor’s IP address - Hostname
The visitor’s IP address and hostname - Referrer
The URL of the page that referred the visitor to the visited page. This is empty when a visitor accesses your site through a bookmark and/or when the referring site hides this information. - Agent
Shows the identifier of the application used to browse or download the visited page. This is where you detect search engine spiders and other bots. - Page
The page that was visited. - Doc_Root
The directory path to the page or script that wrote the page that was viewed. - Script
The script that dynamically wrote the page that was viewed. This will show as index.php for a WordPress site. - Path
This relates to the URL of the visited page as the visitor accessed it. It shows anything in the URL that is written after the name of the script that wrote the visited page but that precedes the query string (if any). - Looking_For
The exact page that the visitor viewed. For example, pages of WordPress sites are written by index.php; this shows the page that WordPress (index.php) served to the visitor. - Keywords
If a visitor came from a search engine, this column states any search terms that were entered into the search engine that caused the search engine to show the visited page in its search results. Not all search engines pass this information on. If the search engine uses HTTPS then your site will need to use HTTPS to receive the data.
Please be aware that the information logged by Really Simple Traffic Logger is passed from a visitor’s browser agent to the server when a visitor arrives. This information can be spoofed. STL will always log a page load regardless of absent information.
An example usage of the log file is to monitor how site visitors move around your website. This is easily accomplished by sorting the data by column C (IP) then column B (TIME). Data is usually archived by date but if a session period covers two dates you might sort the data by column A first.
The Counters
The script produces three counters.
- Session counter
- Previous sessions counter (plural intended)
- Total tally
The session counter logs hits since the start of a configured session period; the previous sessions counter logs all hits from all logged sessions prior to the current session; and the “total tally” registers the sum of the (current) session counter and the previous sessions counter.
You can choose whether or not to display none, any or all of the three counters on your website.
Counter Styling
The counter is formatted as a table. The table has the ID of “simple_php_counter” and the counter labels are encased in span tags with the class ‘simple_php_counter’. Both the table ID and span class make it easy to style the counter’s layout with CSS.
The example CSS styling currently set in the script produces a red box with black counter text, white heading labels and black table borders. If you removed the styling from the script and placed it in your html head, it would look similar to this:
<style> #simple_php_counter { background-color:black; font-size: 12px; } #simple_php_counter td { background-color: red; padding-left: 4px; padding-right: 4px; } .simple_php_counter { color: white; } </style>
The table may be completely replaced by other HTML elements and background images can be used if desired. The code for the table is embedded toward the bottom of the script between the tags “START COUNTER DISPLAY” and “END COUNTER DISPLAY”.
The heading texts may be set in the scripts configs.
Quirks
The program has one quirk that I’m aware of:
A blank cy_archive file or a blank se_archive file is created when the cycle reset time is set to an earlier time than the time reported by the server when the script is first run. This is not an issue in cycle mode as the script will create a new cy_archive file at the start of the next session; it’s not normally an issue when running in session mode as only one log is usually required.
Need Help or Have questions?
Leave questions and comments here and I’ll do my best to answer them within 24 hours.
The Future
A future version of this script will include realtime visitor monitoring in a graphical backend so you will be able to view data without downloading log files. I’ve only left it out of this version because I want to keep the script small. I might remove session mode to reduce the script’s size; it depends on the feedback I receive. I might even add script configuration via the proposed backend to make it easier to style the counter (I’ve recently written a script that would assist with this, I currently use it to display random ads here at JournalXtra. It will be released soon).