PHP获取客户端和服务器IP地址

 1 /**
 2  * 获取客户端IP地址
 3  * @return string
 4  */ 
 5 function get_client_ip() { 
 6     if(getenv('HTTP_CLIENT_IP')){ 
 7         $client_ip = getenv('HTTP_CLIENT_IP'); 
 8     } elseif(getenv('HTTP_X_FORWARDED_FOR')) { 
 9         $client_ip = getenv('HTTP_X_FORWARDED_FOR'); 
10     } elseif(getenv('REMOTE_ADDR')) {
11         $client_ip = getenv('REMOTE_ADDR'); 
12     } else {
13         $client_ip = $_SERVER['REMOTE_ADDR'];
14     } 
15     return $client_ip; 
16 }   
17 /**
18 * 获取服务器端IP地址
19  * @return string
20  */ 
21 function get_server_ip() { 
22     if (isset($_SERVER)) { 
23         if($_SERVER['SERVER_ADDR']) {
24             $server_ip = $_SERVER['SERVER_ADDR']; 
25         } else { 
26             $server_ip = $_SERVER['LOCAL_ADDR']; 
27         } 
28     } else { 
29         $server_ip = getenv('SERVER_ADDR');
30     } 
31     return $server_ip; 
32 }

 

posted @ 2013-09-23 21:26  幻星宇  阅读(399)  评论(0编辑  收藏  举报