Forum Moderators: coopster

Message Too Old, No Replies

IP logging

logging ip address's and proxies

         

Matthew1980

7:19 pm on Jan 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there people of webmaster world,

$_SERVER['HTTP_CLIENT_IP'] && $_SERVER['HTTP_X_FORWARDED_FOR']

Do these globals only run depending on how the PHP has been setup on the machine running it or are there other ways of finding out a terminals *genuine* IP, or am I going about this the wrong way.

I only get joy from using $_SERVER['REMOTE_ADDR'] at the moment, but I am aware that majority of terminals these days run via proxies etc.

I have a function already that stores IP address's in array format, but as yet its only $_SERVER['REMOTE_ADDR'] thats in use, when I echo the other two to screen, I get undefined index error's.

I am trying to add some security by using IP's and logging them for verification.

Cheers,

MRb

dreamcatcher

7:21 am on Jan 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Mrb,

$_SERVER['HTTP_CLIENT_IP'] && $_SERVER['HTTP_X_FORWARDED_FOR'] are only available if they are populated, so you should always use isset [php.net] or empty [php.net] to check.

If IP addresses are stored in $_SERVER['HTTP_X_FORWARDED_FOR'], they may be seperated with a comma. Here`s a simple function you can use:

function getIPAddresses() {
$ip = array();
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip[] = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'],',')!==FALSE) {
$split = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
foreach ($split AS $value) {
$ip[] = $value;
}
} else {
$ip[] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
} else {
$ip[] = $_SERVER['REMOTE_ADDR'];
}
return (!empty($ip) ? implode(',',$ip) : '');
}

$ips = getIPAddresses();
print_r($ips);

dc