php擷取使用者通路ip位址的5種方法:
<?php //方法1:
$ip = $_server["remote_addr"];
echo $ip;
//方法2:
$user_ip = ($_server["http_via"]) ? $_server["http_x_forwarded_for"] : $_server["remote_addr"];
$user_ip = ($user_ip) ? $user_ip : $_server["remote_addr"];
echo $user_ip;
//方法3:
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])) {
$ip = $ips[$i];
break;
}
}
return ($ip ? $ip : $_server['remote_addr']);
}
echo getrealip();
//方法4:
if ($http_server_vars["http_x_forwarded_for"])
$ip = $http_server_vars["http_x_forwarded_for"];
elseif ($http_server_vars["http_client_ip"])
$ip = $http_server_vars["http_client_ip"];
elseif ($http_server_vars["remote_addr"])
$ip = $http_server_vars["remote_addr"];
elseif (getenv("http_x_forwarded_for"))
$ip = getenv("http_x_forwarded_for");
elseif (getenv("http_client_ip"))
$ip = getenv("http_client_ip");
elseif (getenv("remote_addr"))
$ip = getenv("remote_addr");
else
$ip = "unknown";
echo $ip ;
//方法5:
if(getenv('http_client_ip')) {
$onlineip = getenv('http_client_ip');
} elseif(getenv('http_x_forwarded_for')) {
$onlineip = getenv('http_x_forwarded_for');
} elseif(getenv('remote_addr')) {
$onlineip = getenv('remote_addr');
} else {
$onlineip = $http_server_vars['remote_addr'];
echo $onlineip;