PHP 获取ip地址代码汇总

前端之家收集整理的这篇文章主要介绍了PHP 获取ip地址代码汇总前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码一:

PHP;"> function getip() { static $ip = ''; $ip = $_SERVER['REMOTE_ADDR']; if(isset($_SERVER['HTTP_CDN_SRC_IP'])) { $ip = $_SERVER['HTTP_CDN_SRC_IP']; } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/',$_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}#s',$_SERVER['HTTP_X_FORWARDED_FOR'],$matches)) { foreach ($matches[0] AS $xip) { if (!preg_match('#^(10|172\.16|192\.168)\.#',$xip)) { $ip = $xip; break; } } } return $ip; }

代码二:

PHP;">

方法三:

PHP;">

方法四:

方法五:

PHP;"> PHP function get_real_ip() { $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 get_real_ip(); ?>

方法六:

PHP;">
原文链接:https://www.f2er.com/php/21588.html

猜你在找的PHP相关文章