您好,欢迎访问一九零五行业门户网

php cli方式下获取服务器ip_PHP教程

(1)php cli方式下获取服务器ip
[php] 
function getserverip(){ 
        $ss = exec('/sbin/ifconfig eth0 | sed -n \'s/^ *.*addr:\\([0-9.]\\{7,\\}\\) .*$/\\1/p\'',$arr);      
        $ret = $arr[0]; 
        return $ret; 
    } 
function getserverip(){
  $ss = exec('/sbin/ifconfig eth0 | sed -n \'s/^ *.*addr:\\([0-9.]\\{7,\\}\\) .*$/\\1/p\'',$arr);  
  $ret = $arr[0];
  return $ret;
 }
(2)php cgi方式下 获取 客户端ip和服务器端ip
[php]
[php]
php获取客户端和服务器ip地址的函数写法如下: 
[code=php width=600px]/**
* 获取客户端ip地址
* @return string
*/  
function get_client_ip() {  
    if(getenv('http_client_ip')){  
        $client_ip = getenv('http_client_ip');  
    } elseif(getenv('http_x_forwarded_for')) {  
        $client_ip = getenv('http_x_forwarded_for');  
    } elseif(getenv('remote_addr')) { 
        $client_ip = getenv('remote_addr');  
    } else { 
        $client_ip = $_server['remote_addr']; 
    }  
    return $client_ip;  
}    
/**
* 获取服务器端ip地址
* @return string
*/  
function get_server_ip() {  
    if (isset($_server)) {  
        if($_server['server_addr']) { 
            $server_ip = $_server['server_addr'];  
        } else {  
            $server_ip = $_server['local_addr'];  
        }  
    } else {  
        $server_ip = getenv('server_addr'); 
    }  
    return $server_ip;  

[/code] 
客户端ip相关的变量  
1. $_server['remote_addr']; 客户端ip,有可能是用户的ip,也有可能是代理的ip。
2. $_server['http_client_ip']; 代理端的ip,可能存在,可伪造。
3. $_server['http_x_forwarded_for']; 用户是在哪个ip使用的代理,可能存在,可以伪造。
服务器端ip相关的变量  
1. $server_name,需要使用函数gethostbyname()获得。这个变量无论在服务器端还是客户端均能正确显示。
2. $http_server_vars[server_addr],在服务器端测试:127.0.0.1(这个与httpd.conf中bindaddress的设置值相关)。在客户端测试结果正确。
3. $_server['local_addr'] 、$http_server_vars['local_addr'],测试中,未获得任何结果(测试环境php5)。 
完整的获得ip类 
[code=php width=600px]01./**   
02. * get client/server ip    
03. *    
04. * @author  yaron (http://yaron.org.cn)   
05. * @version  0.1   
06. * @package     
07. */      
08.     
09.class  getip{     
10. function  clientip(){     
11.  $cip  =  getenv ( 'remote_addr' );     
12.  $cip1  =  getenv ( 'http_x_forwarded_for' );     
13.  $cip2  =  getenv ( 'http_client_ip' );     
14.  $cip1  ?  $cip  =  $cip1  : null;     
15.  $cip2  ?  $cip  =  $cip2  : null;     
16.  return   $cip ;     
17. }    
18. function  serverip(){     
19.  return   gethostbyname ( $_server_name );     
20. }    
21.}    
22.     
23.$getip   =  new  getip();     
24.$clientip  = getip::clientip();     
25.$serverip  = getip::serverip();     
26.     
27.echo   'client ip is ' , $clientip , '
' ;     
28.echo   'server ip is ' , $serverip , '
' ;   
php获取客户端和服务器ip地址的函数写法如下:
[code=php width=600px]/**
* 获取客户端ip地址
* @return string
*/
function get_client_ip() {
    if(getenv('http_client_ip')){
        $client_ip = getenv('http_client_ip');
    } elseif(getenv('http_x_forwarded_for')) {
        $client_ip = getenv('http_x_forwarded_for');
    } elseif(getenv('remote_addr')) {
        $client_ip = getenv('remote_addr');
    } else {
        $client_ip = $_server['remote_addr'];
    }
    return $client_ip;
}  
/**
* 获取服务器端ip地址
* @return string
*/
function get_server_ip() {
    if (isset($_server)) {
        if($_server['server_addr']) {
            $server_ip = $_server['server_addr'];
        } else {
            $server_ip = $_server['local_addr'];
        }
    } else {
        $server_ip = getenv('server_addr');
    }
    return $server_ip;
}
[/code]
客户端ip相关的变量
1. $_server['remote_addr']; 客户端ip,有可能是用户的ip,也有可能是代理的ip。
2. $_server['http_client_ip']; 代理端的ip,可能存在,可伪造。
3. $_server['http_x_forwarded_for']; 用户是在哪个ip使用的代理,可能存在,可以伪造。
服务器端ip相关的变量
1. $server_name,需要使用函数gethostbyname()获得。这个变量无论在服务器端还是客户端均能正确显示。
2. $http_server_vars[server_addr],在服务器端测试:127.0.0.1(这个与httpd.conf中bindaddress的设置值相关)。在客户端测试结果正确。
3. $_server['local_addr'] 、$http_server_vars['local_addr'],测试中,未获得任何结果(测试环境php5)。
完整的获得ip类
[code=php width=600px]01./**  
02. * get client/server ip   
03. *   
04. * @author  yaron (http://yaron.org.cn)  
05. * @version  0.1  
06. * @package    
07. */    
08.   
09.class  getip{   
10. function  clientip(){   
11.  $cip  =  getenv ( 'remote_addr' );   
12.  $cip1  =  getenv ( 'http_x_forwarded_for' );   
13.  $cip2  =  getenv ( 'http_client_ip' );   
14.  $cip1  ?  $cip  =  $cip1  : null;   
15.  $cip2  ?  $cip  =  $cip2  : null;   
16.  return   $cip ;   
17. }  
18. function  serverip(){   
19.  return   gethostbyname ( $_server_name );   
20. }  
21.}  
22.   
23.$getip   =  new  getip();   
24.$clientip  = getip::clientip();   
25.$serverip  = getip::serverip();   
26.   
27.echo   'client ip is ' , $clientip , '
' ;   
28.echo   'server ip is ' , $serverip , '
' ;
http://www.bkjia.com/phpjc/477381.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/477381.htmltecharticle(1)php cli方式下获取服务器ip [php] function getserverip(){ $ss = exec(/sbin/ifconfig eth0 | sed -n \s/^ *.*addr:\\([0-9.]\\{7,\\}\\) .*$/\\1/p\,$arr); $ret = $arr[0]; r...
其它类似信息

推荐信息