Advanced IP Logger (IP Address, Refer Page, Current Page + More!)

Joined
Jan 19, 2010
Messages
32
Reaction score
82
Logs Records Of:
* IP Address
* Refer Page
* Current Page
* Local Date
* Local Time
* User Agent

Example of a Log:
Code:
[B]Wednesday 11 November 2009 10:10pm[/B]
[B]IP:[/B] 82.3.557.17
[B]Page:[/B] /test.php
[B]Refer:[/B] [URL]http://www.randomsite.com/forum.php?=24924[/URL]
[B]Useragent: [/B]Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)

advancedtracking.php

Code:
<?php
$log = 'log.html';
$ip = $_SERVER['REMOTE_ADDR'];
$page = $_SERVER['REQUEST_URI'];
$refer = $_SERVER['HTTP_REFERER'];
$date_time = date("l j F Y  g:ia", time() - date("Z")) ;
$agent = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen("log.html", "a");
fputs($fp, "
<b>$date_time</b> <br> <b>IP: </b>$ip<br><b>Page: </b>$page<br><b>Refer: </b>$refer<br><b>Useragent: 

</b>$agent <br><br>
");
flock($fp, 3); 
fclose($fp);
?>
 
Between below one detects real ip If they use proxy

PHP:
<?php
// Dev Googler of BHW added Real IP Function 

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$realip = getRealIpAddr();
$log = 'log.html';
$ip = $_SERVER['REMOTE_ADDR'];
$page = $_SERVER['REQUEST_URI'];
$refer = $_SERVER['HTTP_REFERER'];
$date_time = date("l j F Y  g:ia", time() - date("Z")) ;
$agent = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen("log.html", "a");
fputs($fp, "
<b>$date_time</b> <br> <b>IP: </b>$ip<br><b>IP: </b>$realip<br><b>Page: </b>$page<br><b>Refer: </b>$refer<br><b>Useragent: 

</b>$agent <br><br>
");
flock($fp, 3); 
fclose($fp);
?>
 
port 80 and port 8080 proxys will be seen as a real ip regardless of the above.
 
Here is a simple script to distinguish bots and users:

Code:
//Here is sql for mysql db:

CREATE TABLE `bot_ips` (
  `id` int(11) NOT NULL auto_increment,
  `ip` varchar(25) NOT NULL,
  `bot` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `ipi` (`ip`)
) ENGINE=MyISAM

//Fill db with ip:

mysql_connect("localhost","root","");
mysql_select_db("mydatabase");

$ips = array(
'google' => 'hxxp://iplistsdotcom/nw/google.txt',
'msn' => 'hxxp://iplistsdotcom/nw/msn.txt',
);

foreach ($ip_list as $bot => $ip_url) {

    $strs = file($ip_url);
    for($i = 0; $i < count($strs); $i++ )
    {
        if( $strs[$i][0] == '#' ) continue;
        $str = trim($strs[$i],"\r\n ");
        mysql_query("INSERT IGNORE INTO bot_ips SET ip = '".mysql_real_escape_string($str)."', bot = '".$bot."'");
    }

}

//This function returns the name of bot or NULL if user

function isBot()
{
    if( empty($_SERVER['REMOTE_ADDR']) ) return false;

    $res = mysql_query("SELECT bot FROM bot_ips WHERE '".$_SERVER['REMOTE_ADDR']."' LIKE CONCAT(ip,'%') LIMIT 0,1");
    $row = mysql_fetch_assoc($res);

    if( empty($row) ) return false;

    return $row['bot'];
}
 
Why you don't use the Apache's LogFormat directive? You can get any header you want with it. For example you can use "%{HTTP_X_FORWARDED_FOR}i" to get HTTP_X_FORWARDED_FOR value.

Also its always hard to parse multi-lined log files, just use a single line per request so later you can get anything you want with a single grep & awk combination.

I'm not trolling, just trying to help.
 
Back
Top