Webmaster Forum

Go Back   Webmaster Forum > Webmaster Discussion Forums > Web Design and Graphics

Notices

Reply
 
LinkBack Thread Tools Display Modes
Old 05-17-2008, 03:44 PM   #1
New Member
 
Join Date: May 2008
Posts: 14
Credits: 1
Sarunas is on a distinguished road
Default How do I retrieve the remote IP of a user...

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.

Code:
<?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);
}

?>
If you need help contact me

?Sarunas
Sarunas is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[How-To] Create User Feedback HTML Form TheGeek Web Design and Graphics 2 07-17-2008 02:22 AM
Put Ur Site in 185 Bookmarks site emsplanet Services 0 06-24-2008 02:15 PM
[Showcase] EvilGunZ User Page - BETA Aziz Web Design and Graphics 3 06-24-2008 12:17 PM



vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios