PHP入门教程的三种方式来获得真正的第一种方法的用户(客户端)的IP地址相当棘手,但课课家小编我一直在使用这几种方法:
PHP入门教程方法一
01function get_real_ip(){
02 $ip=false;
03 if(!empty($_SERVER['HTTP_CLIENT_IP'])){
04 $ip=$_SERVER['HTTP_CLIENT_IP'];
05 }
06 if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
07 $ips=explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
08 if($ip){ array_unshift($ips, $ip); $ip=FALSE; }
09 for ($i=0; $i < count($ips); $i++){
10 if(!eregi ('^(10│172.16│192.168).', $ips[$i])){
11 $ip=$ips[$i];
12 break;
13 }
14 }
15 }
16 return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
17}
PHP入门教程方法二
查看代码
打印
01function get_real_ip(){
02 static $realip;
03 if(isset($_SERVER)){
04 if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
05 $realip=$_SERVER['HTTP_X_FORWARDED_FOR'];
06 }else if(isset($_SERVER['HTTP_CLIENT_IP'])){
07 $realip=$_SERVER['HTTP_CLIENT_IP'];
08 }else{
09 $realip=$_SERVER['REMOTE_ADDR'];
10 }
11 }else{
12 if(getenv('HTTP_X_FORWARDED_FOR')){
13 $realip=getenv('HTTP_X_FORWARDED_FOR');
14 }else if(getenv('HTTP_CLIENT_IP')){
15 $realip=getenv('HTTP_CLIENT_IP');
16 }else{
17 $realip=getenv('REMOTE_ADDR');
18 }
19 }
20 return $realip;
21}
PHP入门教程方法三
查看代码
打印
01// 获取IP地址(摘自discuz)
02function getIp(){
03 $ip='未知IP';
04 if(!empty($_SERVER['HTTP_CLIENT_IP'])){
05 return is_ip($_SERVER['HTTP_CLIENT_IP'])?$_SERVER['HTTP_CLIENT_IP']:$ip;
06 }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
07 return is_ip($_SERVER['HTTP_X_FORWARDED_FOR'])?$_SERVER['HTTP_X_FORWARDED_FOR']:$ip;
08 }else{
09 return is_ip($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:$ip;
10 }
11}
12function is_ip($str){
13 $ip=explode('.',$str);
14 for($i=0;$i<count($ip);$i++){
15 if($ip[$i]>255){
16 return false;
17 }
18 }
19 return preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/',$str);
来自:网络教育平台_猜您喜欢的php入门教程相关视频教程:|Laravel为Web艺术家设计的PHP框架|
PHP获取用户(客户端)真实IP地址3种办法
2019-06-14