Get Users IP Address
Accounts for proxies:
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip=$_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}
Much helpful in my next online directory project.
Thanks.
very funky
Much helpful in my next online directory project.
Thanks.
nice
Thanks! I’ve been looking for a PHP IP code for ages!
Here’s an easier way:
Its easier but it will not give proper IP address when your user is behind proxy…
This more complete, as I use in one of my site ..
function getRealIP()
{
if( $_SERVER['HTTP_X_FORWARDED_FOR'] != ” )
{
$client_ip =
( !empty($_SERVER['REMOTE_ADDR']) ) ?
$_SERVER['REMOTE_ADDR']
:
( ( !empty($_ENV['REMOTE_ADDR']) ) ?
$_ENV['REMOTE_ADDR']
:
“unknown” );
// Proxies are added at the end of this header
// Ip addresses that are “hiding”. To locate the actual IP
// User begins to look for the beginning to find
// Ip address range that is not private. If not
// Found none is taken as the value REMOTE_ADDR
$entries = split(‘[, ]‘, $_SERVER['HTTP_X_FORWARDED_FOR']);
reset($entries);
while (list(, $entry) = each($entries))
{
$entry = trim($entry);
if ( preg_match(“/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/”, $entry, $ip_list) )
{
// http://www.faqs.org/rfcs/rfc1918.html
$private_ip = array(
‘/^0\./’,
‘/^127\.0\.0\.1/’,
‘/^192\.168\..*/’,
‘/^172\.((1[6-9])|(2[0-9])|(3[0-1]))\..*/’,
‘/^10\..*/’);
$found_ip = preg_replace($private_ip, $client_ip, $ip_list[1]);
if ($client_ip != $found_ip)
{
$client_ip = $found_ip;
break;
}
}
}
}
else
{
$client_ip =
( !empty($_SERVER['REMOTE_ADDR']) ) ?
$_SERVER['REMOTE_ADDR']
:
( ( !empty($_ENV['REMOTE_ADDR']) ) ?
$_ENV['REMOTE_ADDR']
:
“unknown” );
}
return $client_ip;
}
That looks pretty straightforward and simple.
Thanks.