i use the following function in the head of all my files
function db_post_stat()
{
//echo "$sql<BR>";
include "vars.php";
$link = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);
$sess_id = session_id();
$server = $_SERVER['SERVER_NAME'];
$url = $_SERVER['PHP_SELF'];
$query_string = $_SERVER['QUERY_STRING'];
$ip = $_SERVER['REMOTE_ADDR'];
$user = $_SESSION['uname'];
$sql = "INSERT INTO {$table['stats']}(sess_id, time, server, url, query_string, ip, user) VALUES ('$sess_id', NOW(), '$server', '$url', '$query_string', '$ip', '$user')";
//echo $sql . "<BR>";
$result = mysql_query($sql)
or die("Error in stat sql");
mysql_close($link);
return TRUE;
}
This basically connects to a database and posts information about the session, user logged in, ip, time and which server and page they are visiting. You could easily add a variable for http referrer into it. Haven't done the code to display results yet, but if you want i'll post it here.