PHP 获取客户端真实IP地址多种方法小结
程序员文章站
2023-04-07 16:06:48
经过复杂的判断与算是的获取ip地址函数 复制代码 代码如下: function getip() { if (getenv('http_client_ip')) { $ip...
经过复杂的判断与算是的获取ip地址函数
function getip() {
if (getenv('http_client_ip')) {
$ip = getenv('http_client_ip');
}
elseif (getenv('http_x_forwarded_for')) {
$ip = getenv('http_x_forwarded_for');
}
elseif (getenv('http_x_forwarded')) {
$ip = getenv('http_x_forwarded');
}
elseif (getenv('http_forwarded_for')) {
$ip = getenv('http_forwarded_for');
}
elseif (getenv('http_forwarded')) {
$ip = getenv('http_forwarded');
}
else {
$ip = $_server['remote_addr'];
}
return $ip;
}
最简单获取ip地址代码一句实例
$reip=$_server["remote_addr"];
echo $reip;
php获取ip的算法
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;
可以分出内网与外网站ip地址获取程序
function getip_out(){
$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 getip_out();
php获取ip的算法,用了?号表达式来处理
$user_ip = ($_server["http_via"]) ? $_server["http_x_forwarded_for"] : $_server["remote_addr"];
$user_ip = ($user_ip) ? $user_ip : $_server["remote_addr"];
复制代码 代码如下:
function getip() {
if (getenv('http_client_ip')) {
$ip = getenv('http_client_ip');
}
elseif (getenv('http_x_forwarded_for')) {
$ip = getenv('http_x_forwarded_for');
}
elseif (getenv('http_x_forwarded')) {
$ip = getenv('http_x_forwarded');
}
elseif (getenv('http_forwarded_for')) {
$ip = getenv('http_forwarded_for');
}
elseif (getenv('http_forwarded')) {
$ip = getenv('http_forwarded');
}
else {
$ip = $_server['remote_addr'];
}
return $ip;
}
最简单获取ip地址代码一句实例
复制代码 代码如下:
$reip=$_server["remote_addr"];
echo $reip;
php获取ip的算法
复制代码 代码如下:
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;
可以分出内网与外网站ip地址获取程序
复制代码 代码如下:
function getip_out(){
$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 getip_out();
php获取ip的算法,用了?号表达式来处理
复制代码 代码如下:
$user_ip = ($_server["http_via"]) ? $_server["http_x_forwarded_for"] : $_server["remote_addr"];
$user_ip = ($user_ip) ? $user_ip : $_server["remote_addr"];
上一篇: PHP生成UTF8文件的方法