Jump to content
MakeWebGames

[FAQ] How do I retrieve the remote IP of a user


Recommended Posts

Guest Anonymous
Posted

Detecting the remote IP address of a user is a common problem, one that seems to have no definitive answer.

Technically, there are many issues with the detection process, notably the fact the IP address can and do change for clients whenever they reboot their router.

Also, people use proxies which makes it very hard to detect "past" the proxy and obtain the real address.

This small suite of functions with the primary one being remote_ip() is one possible solution.

remote_ip() itself just returns a comma separated list of IPv4 addresses - including both IPs in the private and public range.

 

<?php

//
//  <string> remote_ip( )
//
//  Returns the IP address(s) of the remote user
//  

function remote_ip( )
{
$keys = array
(
	"HTTP_CLIENT_IP",
	"HTTP_X_FORWARDED_FOR",
	"HTTP_X_FORWARDED",
	"HTTP_FORWARDED_FOR",
	"HTTP_FORWARDED",
	"REMOTE_ADDR",
);

$list = array
(
	true  => array(),
	false => array(),
);

foreach ($keys as $key)
	if (isset($_SERVER[$key]) && is_string($_SERVER[$key]))
		foreach (explode(",", $_SERVER[$key]) as $ip)
			if (is_ipv4($ip = trim($ip)))
				$list[is_local_ipv4($ip)][] = $ip;

list($local, $remote) = array_values($list);

usort($remote, "ipcmp");
usort($local,  "ipcmp");

return implode(",", array_merge($remote, $local));
}		

//
//  <boolean> is_ipv4( <string> $ip )
//
//  Returns TRUE if the passed string is an IPv4 address
//

function is_ipv4( $ip )
{
if (!preg_match("`^\d+(\.\d+){3}$`ims", $ip))
	return false;

foreach (explode(".", $ip) as $part)
	if ($part > 255)
		return false;

return true;
}

//
//  <boolean> is_local_ipv4( <string> $ip )
//
//  Returns TRUE if the passed IPv4 address is local (see RFC 1918)
//

function is_local_ipv4( $ip )
{
$table = array
(
	  167772160 => 4278190080,	// (127/8)
	 2130706432 => 4278190080,	// (10/8)
	 2886729728 => 4293918720,	// (172.16/12)
	 3232235520 => 4294901760,	// (196.168/16)
);

$net = iptolong($ip);

foreach ($table as $addr => $mask)
	if (($net & $mask) == $addr)
		return true;

return false;
}

//
//  <integer> iptolong( <string> $ip )
//
//  Replacement for the standard ip2long( ) function
//

function iptolong( $ip )
{
$parts = explode(".", $ip);

return $parts[0] > 127 ? ((ip2long($ip) & 0x7FFFFFFF) + 0x80000000) : ip2long($ip);
}

//
//  <integer> ipcmp( <string> $a, <string> $b )
//
//  Like strcmp(), but for IP addresses
//

function ipcmp( $a, $b )
{
return iptolong($a) - iptolong($b);
}

?>

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...