Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Visitor Info Log
#1
I thought it might be interesting to share with other folks how I am tracking my site visitor info.

Place the following code at the begining of the root directory index.php file.

PHP Code:
/*  +-----------------------------------------------------------------+
    |                           Visitor Log                           |
    +-----------------------------------------------------------------+ */
 
   
 
function get_ip_location($ip) {
 
 $location file_get_contents('http://freegeoip.net/json/'.$ip);
 
 return $location;
 }
 
 
print_r($location);    
    $ip 
"";
 
   if(isset($_SERVER["REMOTE_ADDR"]) ) { 
 
       $ip $_SERVER["REMOTE_ADDR"]; 
 
   
 
   else if(isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) { 
 
       $ip $_SERVER["HTTP_X_FORWARDED_FOR"]; 
 
   
 
   else if(isset($_SERVER["HTTP_CLIENT_IP"]) ) { 
 
       $ip $_SERVER["HTTP_CLIENT_IP"]; 
 
   
 
   else {
 
       $ip ="No IP";
 
   }

 
   if ($ip != "No IP" {
 
     $location get_ip_location ($ip);
 
   } else {
 
     $location=''   
    
}
 
   
    $info  
date('Y.m.d H:i:s');
 
   $info .= "   ".$location;
 
   $f=fopen("visitor.log","a");
 
   fwrite($f,$info);
 
   fclose($f); 
 

Each time your site is visited, it will enter visitor information into a file in the root directory called "visitor.log".
A typical entry looks like this...

2016.02.17 07:15:19   {"ip":"67.61.123.28","country_code":"US","country_name":"United States","region_code":"OR","region_name":"Oregon","city":"Ontario","zip_code":"97914","time_zone":"America/Boise","latitude":44.0861,"longitude":-117.0188,"metro_code":757}

One word of caution before everybody gets all excited. The lookup time to this public database is about 3 seconds, so there is some page load latency .

During high internet usage, this free db may return blank or report "try again later"
My logs show about 1 out of 10 requests fail. And they may fail for periods of 2-3 minutes when I am testing.


This "Jason" data dump can be manipulated by a spreadsheet, database, or custom PHP  program. At some time in the future I will probably write a custom analysis program in PHP. I use it to see daily activity and I find the the country of origin fascinating.

I do not know how to make plugins and don't expect to take the time to learn. But, if someone wants to take the time to convert this to a plugin, I would willingly contribute a db analysis.

A couple of enhancements that some folks might want is to limit the file size by date or size. Since I regularly do backups and ftp into my site, I regularly reset the file.

To trim the file to a certain number of lines, this routine can be used.


Code:
// trim file size

$max_lines = 10000;
$lines = file('visitor.log');
$line_cnt = count($lines);
if ($line_cnt > $max_lines) {
  $delete_line = $line_cnt - $max_lines;
  $lines = array_splice($lines, $delete_line);
 
  // Write to file
  $file = fopen('visitor.log', 'w');
  fwrite($file,implode('', $lines));
  fclose($file);
}
Thanks,
jwzumwalt
(\__/)
(='.'=)
(")_(")
Reply


Messages In This Thread
Visitor Info Log - by jwzumwalt - 2016-02-17, 22:23:25
RE: Visitor Info Log - by Charpy1 - 2016-02-18, 02:46:38
RE: Visitor Info Log - by elubben - 2016-02-19, 06:12:45
RE: Visitor Info Log - by jwzumwalt - 2016-02-19, 06:21:57
RE: Visitor Info Log - by elubben - 2016-02-19, 10:35:49



Users browsing this thread: 1 Guest(s)