PHP获取用户访问IP地址的5种方法
程序员文章站
2024-04-02 12:24:34
本文实例为大家分享了5种php获取用户访问ip地址的方法,供大家参考,具体内容如下
本文实例为大家分享了5种php获取用户访问ip地址的方法,供大家参考,具体内容如下
<?php //方法1: $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: function getrealip() { $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 getrealip(); //方法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程序设计有所帮助。