主页 > 编程资料 > PHP >
发布时间:2017-03-23 作者:apizl 阅读:326次

第一种修改好nginx或者Apache配置

clipboard.png

第二种

HTTP_X_FORWARDED

/**
* 得到IP
* @return string
*/
public 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'];
}
$this->ipPortToIp($ip);//得到IP
return $ip;
}
/**
* ip+port转成IP
* @param type $ip
* @return type
*/
public function ipPortToIp(&$ip) {
$array = explode(':', $ip);
if (count($array) > 1) {
$ip = $array[0];
}
return $ip;
}


文章由爱资料原创本文地址:https://www.apizl.com/archives/view-133853-1.html,转载请以链接形式标明本文地址!
关键字词: