HTTP_CLIENT_IP 是代理伺服器傳送的HTTP頭,HTTP_CLIENT_IP確實存在於http請求的header里。
基本介紹
- 中文名:用戶IP
- 外文名:HTTP_CLIENT_IP
- 相關知識點1:REMOTE_ADDR
- 相關知識點2:HTTP_X_FORWARDED_FOR
HTTP_CLIENT_IP 是代理伺服器傳送的HTTP頭。如果是“超級匿名代理”,則返回none值。
//簡單示例
if(getenv('HTTP_CLIENT_IP')){
$ip = getenv('HTTP_CLIENT_IP');
}
這兩行代碼是取得用戶IP的代碼,存在於http請求的header。
//獲取用戶IP的完整代碼:
function get_client_ip()
{
$IP = '';
if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$IP = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$IP = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$IP = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$IP = $_SERVER['REMOTE_ADDR'];
}
return $IP ? $IP : "unknow";
}
{
$IP = '';
if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$IP = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$IP = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$IP = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$IP = $_SERVER['REMOTE_ADDR'];
}
return $IP ? $IP : "unknow";
}