在没有开代理服务器的情况下,直接使用:
$ip=$_SERVER["REMOTE_ADDR"];
下面是通用的一种写法,都能获取到:
要想透过代理服务器取得客户端的真实IP地址,就要使用getenv("HTTP_X_FORWARDED_FOR")来读取。
functiongetIP() { static$realip; if(isset($_SERVER)){ if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ $realip=$_SERVER["HTTP_X_FORWARDED_FOR"]; }elseif(isset($_SERVER["HTTP_CLIENT_IP"])){ $realip=$_SERVER["HTTP_CLIENT_IP"]; }else{ $realip=$_SERVER["REMOTE_ADDR"]; } }else{ if(getenv("HTTP_X_FORWARDED_FOR")){ $realip=getenv("HTTP_X_FORWARDED_FOR"); }elseif(getenv("HTTP_CLIENT_IP")){ $realip=getenv("HTTP_CLIENT_IP"); }else{ $realip=getenv("REMOTE_ADDR"); } } return$realip; }注意:
$_SERVER和getenv的区别,getenv不支持IIS的isapi方式运行的php
getenv(“REMOTE_ADDR”)函数在apache下能正常获取ip地址,而在iis中没有作用,而$_SERVER['REMOTE_ADDR']函数,既可在apache中成功获取访客的ip地址,在iis下也同样有效
$ip=$_SERVER["REMOTE_ADDR"]; echo$ip;//方法2:
$user_IP=($_SERVER["HTTP_VIA"])?$_SERVER["HTTP_X_FORWARDED_FOR"]:$_SERVER["REMOTE_ADDR"]; $user_IP=($user_IP)?$user_IP:$_SERVER["REMOTE_ADDR"]; echo$user_IP;
//方法3:
functiongetRealIp() { $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']); }echogetRealIp();
//方法4:
if($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]) { $ip=$HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]; } elseif($HTTP_SERVER_VARS["HTTP_CLIENT_IP"]) { $ip=$HTTP_SERVER_VARS["HTTP_CLIENT_IP"]; } elseif($HTTP_SERVER_VARS["REMOTE_ADDR"]) { $ip=$HTTP_SERVER_VARS["REMOTE_ADDR"]; } elseif(getenv("HTTP_X_FORWARDED_FOR")) { $ip=getenv("HTTP_X_FORWARDED_FOR"); } elseif(getenv("HTTP_CLIENT_IP")) { $ip=getenv("HTTP_CLIENT_IP"); } elseif(getenv("REMOTE_ADDR")) { $ip=getenv("REMOTE_ADDR"); } else { $ip="Unknown"; } echo$ip;
//方法5:
if(getenv('HTTP_CLIENT_IP')){ $onlineip=getenv('HTTP_CLIENT_IP'); }elseif(getenv('HTTP_X_FORWARDED_FOR')){ $onlineip=getenv('HTTP_X_FORWARDED_FOR'); }elseif(getenv('REMOTE_ADDR')){ $onlineip=getenv('REMOTE_ADDR'); }else{ $onlineip=$HTTP_SERVER_VARS['REMOTE_ADDR']; } echo$onlineip; //php验证邮箱,代码如下: $email='fengdingbo@gmail.com'; $result=filter_var($email,FILTER_VALIDATE_EMAIL); var_dump($result);//string(20)"fengdingbo@gmail.com" //php验证url地址,代码如下: $url="https://www.jb51.net"; $result=filter_var($url,FILTER_VALIDATE_URL); var_dump($result);//string(25)"https://www.jb51.net" //php验证ip地址,代码如下: $url="192.168.1.110"; $result=filter_var($url,FILTER_VALIDATE_IP); var_dump($result);//string(13)"192.168.1.110"//该方法也可以用来验证ipv6。
$url="2001:DB8:2de::e13"; $result=filter_var($url,FILTER_VALIDATE_IP); var_dump($result);//string(17)"2001:DB8:2de::e13" publicstaticfunctionisEmail($email) { returnpreg_match("/^([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,4}([\.][a-z]{2})?$/i",$email); } publicstaticfunctionisNumber($num) { returnis_numeric($num); } publicstaticfunctionisUrl($url,$preg=false) { if($preg) { $status=preg_match("/^([^:\/\/])+\:\/\/[\w-]+\.[\w-.\?\/]+$/",$url); } else { $status=filter_var($url,FILTER_VALIDATE_URL); } return$status; }