Recently I have been asked to create a simple and free visitor tracker for a website. Normally I would track this with apache access logs, or using PHP to add the visitors to the database. But with this situation they have no access to PHP or the access logs.
In this example ill be using the PHP GD libraries so make sure those are installed unless you plan on using imagemagick.
// / / / Hydrus stat-track (lite) / / Lite version only includes a counter script / / Created by Devin Smith, July 26th 2003 / / php@arzynik.com / / http://hydrus.sourceforge.net / / / //
/* Database Config / / This MUST be filled in correctly */ $sqlserver = "localhost"; $sqluser = "root"; $sqlpass = ""; $sqldb = "hydrus"; $sqlpref = "hydrus_";
/* Default counter values / / These setings can be changed by calling the script with the vars in the url / / ex: 'http://domain.com/script.php?b=000000&f=FFFFFF&z=3&s=1' / $bgcolor = "000000"; / Background color of counter (000000 - FFFFFF)/ $fgcolor = "FFFFFF"; / Text colot of counter (000000 - FFFFFF)/ $size = 5; / Size of test font (1 - 5) / $show = 1; / Display counter (0 or 1)*/
/* Do not modify below this line unless you know what you are doing */ $dbh=mysql_connect ($sqlserver, $sqluser, $sqlpass) or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ($sqldb);
if ($b) $bgcolor = $b; if ($f) $fgcolor = $f; if ($z) $size = $z; if ($s) $show = $s;
if (getenv(HTTP_X_FORWARDED_FOR)) $ip = getenv(HTTP_X_FORWARDED_FOR); else if (getenv(HTTP_CLIENT_IP)) $ip = getenv(HTTP_CLIENT_IP); else $ip = getenv(REMOTE_ADDR);
$httref = getenv(HTTP_REFERER); $http = "http://".$HTTP_HOST.$REQUEST_URI; $date = date("YmdHis");
/* Browser detect */ if((ereg("Nav", $_SERVER["HTTP_USER_AGENT"])) || (ereg("Gold", $_SERVER["HTTP_USER_AGENT"])) || (ereg("X11", $_SERVER["HTTP_USER_AGENT"])) || (ereg("Mozilla", $_SERVER["HTTP_USER_AGENT"])) || (ereg("Netscape", $_SERVER["HTTP_USER_AGENT"])) AND (!ereg("MSIE", $_SERVER["HTTP_USER_AGENT"]) AND (!ereg("Konqueror", $_SERVER["HTTP_USER_AGENT"])))) $browser = "Netscape"; elseif(ereg("MSIE", $_SERVER["HTTP_USER_AGENT"])) $browser = "MSIE"; elseif(ereg("Lynx", $_SERVER["HTTP_USER_AGENT"])) $browser = "Lynx"; elseif(ereg("Opera", $_SERVER["HTTP_USER_AGENT"])) $browser = "Opera"; elseif(ereg("WebTV", $_SERVER["HTTP_USER_AGENT"])) $browser = "WebTV"; elseif(ereg("Konqueror", $_SERVER["HTTP_USER_AGENT"])) $browser = "Konqueror"; elseif((eregi("bot", $_SERVER["HTTP_USER_AGENT"])) || (ereg("Google", $_SERVER["HTTP_USER_AGENT"])) || (ereg("Slurp", $_SERVER["HTTP_USER_AGENT"])) || (ereg("Scooter", $_SERVER["HTTP_USER_AGENT"])) || (eregi("Spider", $_SERVER["HTTP_USER_AGENT"])) || (eregi("Infoseek", $_SERVER["HTTP_USER_AGENT"]))) $browser = "Bot"; else $browser = "Other";
/* OS detect */ if(ereg("Win", $_SERVER["HTTP_USER_AGENT"])) $os = "Windows"; elseif((ereg("Mac", $_SERVER["HTTP_USER_AGENT"])) || (ereg("PPC", $_SERVER["HTTP_USER_AGENT"]))) $os = "Mac"; elseif(ereg("Linux", $_SERVER["HTTP_USER_AGENT"])) $os = "Linux"; elseif(ereg("FreeBSD", $_SERVER["HTTP_USER_AGENT"])) $os = "FreeBSD"; elseif(ereg("SunOS", $_SERVER["HTTP_USER_AGENT"])) $os = "SunOS"; elseif(ereg("IRIX", $_SERVER["HTTP_USER_AGENT"])) $os = "IRIX"; elseif(ereg("BeOS", $_SERVER["HTTP_USER_AGENT"])) $os = "BeOS"; elseif(ereg("OS/2", $_SERVER["HTTP_USER_AGENT"])) $os = "OS/2"; elseif(ereg("AIX", $_SERVER["HTTP_USER_AGENT"])) $os = "AIX"; else $os = "Other";
$msql = mysql_query("INSERT INTO ".$sqlpref."stats VALUES ('','$http','$httref','$ip','$os','$browser','$date')")or die(mysql_error());
Header("Content-type: image/jpeg"); if ($show) { $viewcount = mysql_num_rows(mysql_query("SELECT id FROM ".$sqlpref."stats WHERE http='$http'")); if ($size >= 5) $sizewidth = 9; elseif ($size >= 4) $sizewidth = 8; elseif ($size >= 3) $sizewidth = 7; elseif ($size >= 2) $sizewidth = 6; else $sizewidth = 5; $imwidth = strlen($viewcount)*$sizewidth; if ($size >= 4) $imheight = 16; elseif ($size >= 2) $imheight = 14; else $imheight = 8; $im = imagecreate ($imwidth, $imheight); $bgcolor = eregi_replace("#","",$bgcolor); $fgcolor = eregi_replace("#","",$fgcolor); $bgr = hexdec($bgcolor{0}.$bgcolor{1}); $bgg = hexdec($bgcolor{2}.$bgcolor{3}); $bgb = hexdec($bgcolor{4}.$bgcolor{5}); $fgr = hexdec($fgcolor{0}.$fgcolor{1}); $fgg = hexdec($fgcolor{2}.$fgcolor{3}); $fgb = hexdec($fgcolor{4}.$fgcolor{5}); $background_color = imagecolorallocate ($im, $bgr, $bgg, $bgb); $text_color = imagecolorallocate ($im, $fgr, $fgg, $fgb); imagestring ($im, $size, 0, 0, $viewcount, $text_color); Imagejpeg($im); ImageDestroy($im); }
Once you have the tracking data you can do whatever you want with it in terms of reports. Take a look at Hydrus Stat Tracker for some simple graphing examples.