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
#2
Thanks for sharing!
Yeah it could be very nice to have this informations somewhere on the backend panel.

I'm using a Piwik instance to have these information but a little plugin like this could be just fine.
__
Contact me if you need a french translation file for your plugin.
Reply
#3
Placed the code into my index.php file, and the only data that I get is 2016.02.17 07:15:19. The other data would be great and I'm wondering if I may have done something wrong?

Gene
Reply
#4
(2016-02-19, 06:12:45)elubben Wrote: Placed the code into my index.php file, and the only data that I get is 2016.02.17 07:15:19. The other data would be great and I'm wondering if I may have done something wrong?

Gene

Thanks for the reply... no there is nothing wrong.

This is a good question and I should have explained this.
During high internet usage, the db will be blank or report "try again later"
When I look at my log, about 1 out of 10 requests fail. And they may fail for periods of 3-4 minutes when I am testing.

I do not know who maintains this free service but it is the only one I know about. Other companies that provide this service charge for each access. So, this is the best I have come across.

Sorry I did not make this clear - I will edit my original post.
Thanks,
jwzumwalt
(\__/)
(='.'=)
(")_(")
Reply
#5
(2016-02-19, 06:21:57)jwzumwalt Wrote:
(2016-02-19, 06:12:45)elubben Wrote: Placed the code into my index.php file, and the only data that I get is 2016.02.17 07:15:19. The other data would be great and I'm wondering if I may have done something wrong?

Gene

Thanks for the reply... no there is nothing wrong.

This is a good question and I should have explained this.
During high internet usage, the db will be blank or report "try again later"
When I look at my log, about 1 out of 10 requests fail. And they may fail for periods of 3-4 minutes when I am testing.

I do not know who maintains this free service but it is the only one I know about. Other companies that provide this service charge for each access. So, this is the best I have come across.

Sorry I did not make this clear - I will edit my original post.
Great, it's working then!!! Thought I did something wrong in pasting to my index.php Smile
Gene
Reply




Users browsing this thread: 1 Guest(s)
Free HDTV Channels Residential Solar Panels