<?php
function getrealip()
{
$ip = FALSE;
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) {
array_unshift($ips, $ip);
$ip = FALSE;
}
for ($i = 0; $i < count($ips); $i++) {
if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {
if (version_compare(phpversion(), "5.0.0", ">=")) {
if (ip2long($ips[$i]) != false) {
$ip = $ips[$i];
break;
}
} else {
if (ip2long($ips[$i]) != -1) {
$ip = $ips[$i];
break;
}
}
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
?>
星期一, 8月 01, 2005
A better way to detect IP in PHP
Sometimes we cannot get the correct IP of the client by $_SERVER['REMOTE_ADDR'], so we need the following function!
訂閱:
文章 (Atom)