sameIP() is my most important function in Power Phlogger. Phlogger calls that
function every time someone enters a page. Actually, that’s all it does: sameIP()
returns true
if the visitor was already logged and doesn’t
need to be tracked again. I’m checking his IP and keep it for the last $timeout
minutes (as default I use 30 minutes). If it’s a new visitor or his timeout has
expired, the function returns false
and I create a new hit.
To make sure noone gets counted twice, I keep all IP’s of current online users.
here’s a sample how to call that function:
1 2 3 4 5 6 7 |
if (!sameIP()) { //track the visitor } else { //don't do anything } |
ipcheck_file.php: (WARNING: this function requires a writable text-file, $ipfile)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
function sameIP() { global $timeout,$ipfile,$REMOTE_ADDR; if (file_exists($ipfile)) { $ip_temparray = file($ipfile); //check for IP-entries in $ipfile. //If they match, check if time has overrun the timeout for($ip_index = 0; $ip_index < count($ip_temparray); $ip_index++) { $ip_entry = explode("|",$ip_temparray[$ip_index]); $oldTime = $ip_entry[1]; $timeoutTime = (time()-$timeout); if(!strcmp($ip_entry[0],$REMOTE_ADDR)) { //--->(1)REMOTE_ADDR maches the file $ip_entry[1] = time(); $ip_temparray[$ip_index] = implode($ip_entry,"|"); $ip_fp = fopen($ipfile,"w"); if(flock($ip_fp,2)) { for($i = 0; $i < count($ip_temparray); $i++) fputs($ip_fp,$ip_temparray[$i]); } else exit("ipfile error: flock write failure!"); if(flock($ip_fp,3)) fclose($ip_fp); else exit("ipfile error: flock release failure!"); if ($oldTime<(time()-$timeout)) { return false; //time expired } else return true; //time ok } else { //if there's no matching IP in your file if ($oldTime<$timeoutTime) { //delete all old IPs $ip_temparray[$ip_index] = ""; $ip_fp = fopen($ipfile,"w"); if(flock($ip_fp,2)) { for($i = 0; $i < count($ip_temparray); $i++) fputs($ip_fp,$ip_temparray[$i]); } else exit("ipfile error: flock write failure!"); if(flock($ip_fp,3)) fclose($ip_fp); else exit("ipfile error: flock release failure!"); } } } //create a new IP-entry in $ipfile $newTime = time(); //--->(2) no IP maches REMOTE_ADDR $entry = "$REMOTE_ADDR|$newTime|\n"; $ip_fp = fopen($ipfile,"a"); if(flock($ip_fp,2)) fputs($ip_fp,$entry,256); else exit("ipfile error: flock write failure!"); if(flock($ip_fp,3)) fclose($ip_fp); else exit("ipfile error: flock release failure!"); return false; } } |
…then I started to get into mySQL, what makes that thing much easier. Use this
function if you got access to a mySQL database.
Here’s my table-definition:
1 2 3 4 5 6 7 8 9 10 |
CREATE TABLE ipcheck ( logid char(13) NOT NULL, ip varchar(15) NOT NULL, t_reload timestamp(14) NOT NULL, t_since timestamp(14) NOT NULL, url varchar(255) NOT NULL, PRIMARY KEY (logid) ); |
ipcheck_mysql.php:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function sameIP() { global $timeout,$REMOTE_ADDR; // delete all old entries where timeout has expired $del_sql = "DELETE FROM ipcheck " . "WHERE UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(t_reload) > ".$timeout.";"; $del_res = mysql_query($del_sql); $sql = "SELECT t_reload " . "FROM ipcheck WHERE ip='".$REMOTE_ADDR."';"; $res = mysql_query($sql); if (!mysql_affected_rows()) { //NEW IP $sql = "INSERT INTO ".$id."_ipcheck (ip,t_since) " . "VALUES ('".$REMOTE_ADDR."',NOW());"; $res = mysql_query($sql); return false; } else { //SAME IP, SAME TIME $sql = "UPDATE ipcheck SET t_reload=NOW() " . "WHERE ip='".$REMOTE_ADDR."';"; $res = mysql_query($sql); return true; } } |