本教程提供几款获取IP地址的代码,各有各的优点,这里获取用户ip 是利用php的全局变量实现的,下面来看看有没有你要找的,php实例代码如下:
-
-
- $ip = $_server["remote_addr"];
- echo $ip;
-
-
-
- function get_real_ip() {
- $ip=false;
- if(!emptyempty($_server["http_client_ip"]))
- {
- $ip = $_server["http_client_ip"];
- }
- if (!emptyempty($_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();
-
-
-
- $ip =$_server["remote_addr"];
- $port = $_server['remote_port'];
- echo $ip.$port;
-
-
-
- 代码如下 复制代码
- 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;
- echo "
- ";
-
-
-
- function get_ip_place(){
- $ip=file_get_contents("http://fw.qq.com/ipaddress");
- $ip=str_replace('"',' ',$ip);
- $ip2=explode("(",$ip);
- $a=substr($ip2[1],0,-2);
- $b=explode(",",$a);
- return $b;
- }
- $ip=get_ip_place();
- print_r($ip);
|