获取客户端ip地址
简单方法:
$_SERVER[‘REMOTE_ADDR’];
getenv('REMOTE_ADDR');
复杂全面方法
public function getClientIp($checkProxy = true)
{
if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) {
$ip = $this->getServer('HTTP_CLIENT_IP');
} else if ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) {
$ip = $this->getServer('HTTP_X_FORWARDED_FOR');
} else {
$ip = $this->getServer('REMOTE_ADDR');
}
return $ip;
}
public function getServer($key = null, $default = null)
{
if (null === $key) {
return $_SERVER;
}
return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default;
}
获取服务器端ip地址
简单方法:
$_SERVER[‘SERVER_ADDR’];
或者 getenv('SERVER_ADDR');