下面是一个常见的获取用户ip的函数:
<?php
function getip()
{
if(getenv("HTTP_CLIENT_IP") && getenv("HTTP_CLIENT_IP")!="unknown")
$ip = getenv("HTTP_CLIENT_IP");
elseif(getenv("HTTP_X_FORWARDED_FOR") && getenv("HTTP_X_FORWARDED_FOR")!="unknown")
$ip = getenv("HTTP_X_FORWARDED_FOR");
elseif(getenv("REMOTE_ADDR") && getenv("REMOTE_ADDR")!="unknown")
$ip = getenv("REMOTE_ADDR");
else
$ip = "none";
return $ip;
}
?>
下面给出一个测试脚本:
<?php
$ch = curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_HTTPHEADER,array(‘X_FORWARDED_FOR: 158.10.10.10’));
curl_setopt($ch,CURLOPT_HTTPHEADER,array(‘CLIENT_IP: 159.10.10.10’));
curl_setopt($ch, CURLOPT_URL,"http://10.10.10.21/test.php");
echo curl_exec($ch);
curl_close($ch);
exit;
?>