Client IP log using X-Forwarded-For

Hi all,

I currently use a PHP script to log the IP of a client. The limitation is if the client is using a VPN or Proxy. I have tried using X-Forwarded-For header with no success. I simply cannot get the IP to log at all. I was wondering if anyone could help me out. Here is my current script -

[php]<?php

date_default_timezone_set(‘Australia/Sydney’);
$iplogfile = ‘log.txt’;
$ipaddress = $_SERVER[‘REMOTE_ADDR’];
$file = file_get_contents($iplogfile);
if ( ! preg_match("/$ipaddress/", $file )) {
$webpage = $_SERVER[‘SCRIPT_NAME’];
$timestamp = date(‘d/m/Y H:i:s’);
$browser = $_SERVER[‘HTTP_USER_AGENT’];
$fp = fopen($iplogfile, ‘a+’);
fwrite($fp, ‘[’.$timestamp.’]: ‘.$ipaddress.’ '.$browser. “\r\n”);
fclose($fp);
}

?>
[/php]

Well, it is sometimes hard to track the real IP address as they can be faked. Lots of code online for faking IP addresses.
But, here is a simple function that seems to work for most cases. Might help. If only needed once in your site, you could
drop the function and just use the code directly. Hope it helps!
[php]
function getRealUserIp(){
switch(true){
case (!empty($_SERVER[‘HTTP_X_REAL_IP’])) : return $_SERVER[‘HTTP_X_REAL_IP’];
case (!empty($_SERVER[‘HTTP_CLIENT_IP’])) : return $_SERVER[‘HTTP_CLIENT_IP’];
case (!empty($_SERVER[‘HTTP_X_FORWARDED_FOR’])) : return $_SERVER[‘HTTP_X_FORWARDED_FOR’];
default : return $_SERVER[‘REMOTE_ADDR’];
}
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service