<?php
new logger();
class logger{
private $totalVisits = '';
private $lastVisit = '';
private $ip = '';
private $tableName = '';
public function __construct(){
$this->populateIP();
$this->getVisitorData();
$this->logVisit();
}
private function getLastVisit(){
$sql = "Select lastVisit, totalVisits from {$this->tableName} where ipAddress='%s'";
$result = mysql_query(sprintf($sql, $this->ip));
$row = mysql_fetch_assoc($result);
if ($row){
$this->lastVisit = $row['lastVisit'];
$this->totalVisits = $row['totalVisits'];
}
}
private function logVisit(){
if (($this->lastVisit + $this->timeOut) > time()){
//log visit but don't bother incrementing counter
$sql = "Update {$this->tableName} set lastVisit = '%s', totalVisits = '%s' where ipAddress='%s'";
mysql_query(sprintf($sql, time(), $this->totalVisits, $this->ip));
} else {
if (empty($this->lastVisit)){
//no records in the database
$sql = "Insert into {$this->tableName} (ipAddress, lastVisit, totalVisits) values ('%s', '%s', '%s')";
mysql_query(sprintf($sql, $this->ip, time(), 1));
} else {
//just update the database
$sql = "Update {$this->tableName} set lastVisit = '%s', totalVisits = '%s' where ipAddress='%s'";
mysql_query(sprintf($sql, time(), $this->totalVisits++, $this->ip));
}
}
}
private function populateIP(){
//taken from [URL unfurl="true"]http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html[/URL]
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}
$this->ip = $ip;
}
}
?>