php 获取用户ip地址的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6717926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Function to get user ip address
提问by Frank Nwoko
Possible Duplicate:
What is the most accurate way to retrieve a user's correct IP address in PHP?
Is there any better function in php to get user ip address? this is what i use at the moment
php中有没有更好的函数来获取用户ip地址?这是我目前使用的
function GetIP()
{
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return($ip);
}
采纳答案by cwallenpoole
Well, your function should behave as expected, but here are some suggestions:
好吧,您的函数应该按预期运行,但这里有一些建议:
// lowercase first letter of functions. It is more standard for PHP
function getIP()
{
// populate a local variable to avoid extra function calls.
// NOTE: use of getenv is not as common as use of $_SERVER.
// because of this use of $_SERVER is recommended, but
// for consistency, I'll use getenv below
$tmp = getenv("HTTP_CLIENT_IP");
// you DON'T want the HTTP_CLIENT_ID to equal unknown. That said, I don't
// believe it ever will (same for all below)
if ( $tmp && !strcasecmp( $tmp, "unknown"))
return $tmp;
$tmp = getenv("HTTP_X_FORWARDED_FOR");
if( $tmp && !strcasecmp( $tmp, "unknown"))
return $tmp
// no sense in testing SERVER after this.
// $_SERVER[ 'REMOTE_ADDR' ] == gentenv( 'REMOTE_ADDR' );
$tmp = getenv("REMOTE_ADDR");
if($tmp && !strcasecmp($tmp, "unknown"))
return $tmp;
return("unknown");
}
回答by Alix Axel
Adapted from this answer:
改编自这个答案:
function GetIP()
{
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key)
{
if (array_key_exists($key, $_SERVER) === true)
{
foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip)
{
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false)
{
return $ip;
}
}
}
}
}
Checks for IPs (in order) in:
检查以下 IP(按顺序):
HTTP_CLIENT_IP
HTTP_X_FORWARDED_FOR
HTTP_X_FORWARDED
HTTP_X_CLUSTER_CLIENT_IP
HTTP_FORWARDED_FOR
HTTP_FORWARDED
REMOTE_ADDR
HTTP_CLIENT_IP
HTTP_X_FORWARDED_FOR
HTTP_X_FORWARDED
HTTP_X_CLUSTER_CLIENT_IP
HTTP_FORWARDED_FOR
HTTP_FORWARDED
REMOTE_ADDR
Remember that the only IP address you can trust is the one coming from $_SERVER['REMOTE_ADDR']
.
请记住,您唯一可以信任的 IP 地址是来自$_SERVER['REMOTE_ADDR']
.