2

Get Notification on Website Visit

Posted (Updated ) in Linux, PHP

This tutorial will show *NIX users how to get a nice notification displaying on their screen when people visit their site.

Disclaimer: This isn’t practical for sites with large numbers of visitors but it’s great for people running small sites on their local machines.

Requirements:

  • Apache with access logging enabled (Any web server that makes log files should work)
  • Python
  • PHP
  • *NIX based machine


I’ve broken this application up into 2 sections – a PHP file that we’ll keep running that consistently monitors the Apache access log for changes and a Python file to display the visitor notification. Let’s get to it.

Create a folder notify and inside a make a new PHP file called notify.php.

Here’s our PHP code

<?php
	$last_ip = '';
	$last_timestamp = 0;
 
	while ( true )
	{
		extract(get_last_visitor());
 
		//Don't tell us when we're viewing our own site
		if ( !in_array($ip, array('127.0.0.1', '::1')) )
		{
			if ( $ip != $last_ip ||
				 $ip == $last_ip && $time > $last_timestamp + 30 )
			{
				passthru(dirname(__FILE__)."/notify.py \"$ip at ".date('g:i:sA', $time)."\"");
				echo "New visitor: $ip at " . date('g:i:sA', $time) . "\n";
			}
		}
 
		//Update last visitor
		$last_ip = $ip;
		$last_timestamp = $time;
 
		sleep(5);
	}
 
	function get_last_visitor()
	{
		$response = '';
		ob_start();
		passthru( "tail /var/log/apache2/access.log -n 1");
		$response = ob_get_clean();
 
		//Clean out crap - everything other than IP and timestamp
		$response = substr($response, 0, strpos($response, '"')-2);
 
		//Get IP
		$ip = trim(substr($response, 0, strpos($response, '-')));
 
		//Remove IP from response so we can grab timestamp
		$response = substr($response, strlen($ip)+6);
 
		//Convert time to unixtime
		$time = strtotime($response);
 
		return compact('ip', 'time');
	}

A bit of explanation:

Firstly I created a function get_last_visitor(). This function retrieves the last line from our apache log file – the last line being the last person to access our site. This line includes all the information we require; the users IP and the time they visited the site.

Line 5 is our main program loop. The program will continue running until we kill it
Line 7 grabs the relevant information of the last visitor to view our site
Line 10 will stop notifications appearing when it’s us viewing our own site on localhost
Line 12 will allow for display of notification if the last visitor to our site has changed since our previous lookup
Line 13 will allow for display of notification if the last visitor is the same as in our previous lookup but they have waited 30 seconds before viewing a new page (This ones really up to you – I find it useful to know if a visitor is still wandering around the site or not)
Line 15 fires the notification script if the previous conditions have been met

Hard stuff out the way – now for the python script to actually display the notification. I’ve set the python script up such that we’re able to just call notify.py <string> – “string” being the message we want to display. Here’s the script:

#! /usr/bin/env python
 
import pynotify
import sys
 
pynotify.init('foo');
pynotify.Notification('New Site Visitor', sys.argv[1]).show()

Line 1 allows us to execute the script by simply typing notify.py
Lines 3-4 import our required libraries
Lines 6-7 generate and display the notification.

…and that’s all there is to it. I’ve attached the completed script for anyone who is interested. You may need to change the path of your apache log files and the time between same-user visits if 30 secs doesn’t suit you. Hope someone finds this as useful as I have.

Downloaded completed script:
Visitor Notification Script.