For we can do nothing against the truth, but for the truth.
2 Corinthians 13:8
Visit statistics are a necessary element of analysis for the performance of any web page. It can include various indicators, the most important of which are the number of visits for a certain period of time, the IP address and the location of the visitor. Here I will present a simple yet effective solution to track your website visitors.
Information about visit time, IP address, country, region, town and zip code is provided. Quite enough isn’t it!
The tracking system consists of three files that exchange data. For experiment purposes, you can place them in the root directory of your website.
Catch the visitor
Each visitor has a unique IP address (indicating the connection point on the network). If we intercept this address it will be easy to determine the other data as well. We will acquire this additional data from an external provider – IPinfoDB. In order to implement this system on your website, you need to register on IPInfoDB and get an API access key. Registration and key are free.
The first system file (statistic-visitors.html) contains JavaScript functions that perform the following tasks:
- Constructs a request to IPInfoDB, including the API address, the key and the format of the response (in this case JSON).
- The sent request automatically includes the IP address of the sender (in the example, the one who opens the page with the JavaScript code of the system).
- Several variables are created containing the date and time of the visit.
- This data is bundled together and sent using the XMLHttpRequest object to the next page count-visitors.php, which will process it and save it to a file.
statistic-visitors.html
<!DOCTYPE html>
<html>
<head>
<title>Statistics visitors</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js" integrity="sha512-DUC8yqWf7ez3JD1jszxCWSVB0DMP78eOyBpMa5aJki1bIRARykviOuImIczkxlj1KhVSyS16w2FSQetkD4UU2w==" crossorigin="anonymous"></script>
</head>
<body>
<h1>Counting visitors</h1>
<h2>Refresh the page to see more results</h2>
<script>
$(window).load(function(){
var api = 'https://api.ipinfodb.com/v3/ip-city/?';
var key = 'key=PUT YOUR KEY HERE';
var format = 'format=json';
var fullURL = api + key + '&' + format;
$.getJSON(fullURL, function(dataVisitorJSON) {
var dataVisitorInner = JSON.stringify(dataVisitorJSON, null, 2);
sessionStorage.setItem('dataVisitor', dataVisitorInner);
});
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ':' + today.getMinutes() + ':' + today.getSeconds() + '\n';
var dateTime = date + ' ' + time;
var data = new FormData();
data.append('data', '\n' + dateTime + sessionStorage.getItem('dataVisitor') + '\n');
var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject('Microsoft.XMLHTTP');
xhr.open('post', '/count-visitors.php', true);
xhr.send(data);
});
</script>
<a href="/visitors.txt">Check visitors</a>
<a href="/count-visitors.php">Check quota</a>
</body>
</html>
Create the records
The second task after receiving the visitor data is to be able to process and record it in a suitable form. The responsibility takes count-visitors.php. For the example, I will use a simple text file called visitors.txt, which I will add to the data of each new visitor. Each visitor record consists of fifteen lines. To keep the records file from getting too big, I’ll limit it to one hundred visitors. Once this limit is reached, the file is automatically purged.
count-visitors.php
<?php
if(!empty($_POST['data'])) {
$data = $_POST['data'];
$fname = 'visitors.txt';
$file = fopen($fname, 'a');
fwrite($file, $data);
fclose($file);
}
$file = new \SplFileObject('visitors.txt', 'r');
$quota = 1500;
$file->seek(PHP_INT_MAX);
echo '<h1>Visitors quota</h1>';
echo '<h2>Counted ';
echo ceil(($file->key() + 1)/15); // count the lines and divide them by fifteen per visitor.
echo ' from ' . ceil($quota / 15) . ' quota</h2>';
if (($file->key() + 1) > $quota) {
$filePointer = fopen('visitors.txt', 'w+');
// Use unlink() function to delete a file
if (!unlink($filePointer)) {
echo ('$filePointer cannot be deleted due to an error');
}
else { echo ('$filePointer has been deleted'); }
}
Test the system
I have already implemented the sample system in my blog. To test click Check Visitors Demo
Comments