首页 >后端开发 >php教程 >php获取本土实际IP

php获取本土实际IP

WBOY
WBOY原创
2016-06-13 10:49:39895浏览

php获取本地实际IP

?函数 real_ip()
{
??? static $realip = NULL;

??? if ($realip !== NULL)
??? {
???????返回 $realip;
??? }

??? if (isset($_SERVER))
??? {
??????? if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
?????? {
???????????? $arr = Explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);

???????????? foreach ($arr AS $ip)
???????????? {
??????????????? $ip = 修剪($ip);

??????????????? if ($ip != 'unknown')
??????????????? {
????????????????????? $realip = $ip;

??????????????????打破;
??????????????? }
???????????? }
??????? }
??????? elseif (isset($_SERVER['HTTP_CLIENT_IP']))
??????? {
???????????? $realip = $_SERVER['HTTP_CLIENT_IP'];
?????? }
???????其他
??????? {
???????????? if (isset($_SERVER['REMOTE_ADDR']))
???????????? {
??????????????? $realip = $_SERVER['REMOTE_ADDR'];
???????????? }
????????????其他
???????????? {
??????????????? $realip = '0.0.0.0';
???????????? }
??????? }
??? }
???否则
??? {
??????? if (getenv('HTTP_X_FORWARDED_FOR'))
?????? {
???????????? $realip = getenv('HTTP_X_FORWARDED_FOR');
??????? }
??????? elseif (getenv('HTTP_CLIENT_IP'))
?????? {
???????????? $realip = getenv('HTTP_CLIENT_IP');
??????? }
???????其他
??????? {
???????????? $realip = getenv('REMOTE_ADDR');
??????? }
??? }

??? preg_match("/[d.]{7,15}/", $realip, $onlineip);
??? $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';

???返回 $realip;
}

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn