PHP获取真实IP地址(获取本地127.0.0.1真实地址)
PHP获取真实IP地址
public function get_real_ip(){ static $realip; if(isset($_SERVER)){ if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){ $realip=$_SERVER['HTTP_X_FORWARDED_FOR']; }else if(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'); }else if(getenv('HTTP_CLIENT_IP')){ $realip=getenv('HTTP_CLIENT_IP'); }else{ $realip=getenv('REMOTE_ADDR'); } } return $realip; } public function get_real_ip__() { $ip=FALSE; //客户端IP 或 NONE if(!empty($_SERVER["HTTP_CLIENT_IP"])){ $ip = $_SERVER["HTTP_CLIENT_IP"]; } //多重代理服务器下的客户端真实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 <($ips); $i++) { if (!eregi("^(10│172.16│192.168).", $ips[$i])) { $ip = $ips[$i]; break; } } } //客户端IP 或 (最后一个)代理服务器 IP return ($ip ? $ip : $_SERVER['REMOTE_ADDR']); } public 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(!preg_match ('/^(10│172.16│192.168)./', $ips[$i])){ $ip=$ips[$i]; break; } } } return ($ip ? $ip : $_SERVER['REMOTE_ADDR']); } public function getLocalIP() { $preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/"; //获取操作系统为win2000/xp、win7的本机IP真实地址 exec("ipconfig", $out, $stats); if (!empty($out)) { foreach ($out AS $row) { if (strstr($row, "IP") && strstr($row, ":") && !strstr($row, "IPv6")) { $tmpIp = explode(":", $row); if (preg_match($preg, trim($tmpIp[1]))) { return trim($tmpIp[1]); } } } } //获取操作系统为linux类型的本机IP真实地址 exec("ifconfig", $out, $stats); if (!empty($out)) { if (isset($out[1]) && strstr($out[1], 'addr:')) { $tmpArray = explode(":", $out[1]); $tmpIp = explode(" ", $tmpArray[1]); if (preg_match($preg, trim($tmpIp[0]))) { return trim($tmpIp[0]); } } } return '127.0.0.1'; }