(1) obtain the Server ip address in php cli Mode
[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) obtain the Client ip address and Server ip address in php cgi Mode
[Php]
[Php]
PHP functions for obtaining client and server IP addresses are as follows:
[Code = Php width = 600px]/**
* Obtain the Client IP Address
* @ 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;
}
/**
* Obtain the Server IP Address
* @ 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]
Client IP-related variables
1. $ _ SERVER ['remote _ ADDR ']; Client IP address, which may be the user IP address or the proxy IP address.
2. $ _ SERVER ['HTTP _ CLIENT_IP ']; proxy IP address, which may exist and can be forged.
3. $ _ SERVER ['HTTP _ X_FORWARDED_FOR ']; the proxy of the user's IP address, which may exist and can be forged.
Server IP-related variables
1. $ SERVER_NAME, which must be obtained using the gethostbyname () function. This variable can be correctly displayed on both the server side and the client side.
2. $ HTTP_SERVER_VARS ["SERVER_ADDR"], test on the server: 127.0.0.1 (this is related to the BindAddress setting value in httpd. conf ). The test results on the client are correct.
3. $ _ SERVER ['local _ ADDR '] and $ HTTP_SERVER_VARS ['local _ ADDR']. No results were obtained during the test (PHP5 test environment ).
Obtain the complete IP class
[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,' <br/> ';
28. echo 'server IP is ', $ serverIp,' <br/> ';
PHP functions for obtaining client and server IP addresses are as follows:
[Code = Php width = 600px]/**
* Obtain the Client IP Address
* @ 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;
}
/**
* Obtain the Server IP Address
* @ 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]
Client IP-related variables
1. $ _ SERVER ['remote _ ADDR ']; Client IP address, which may be the user IP address or the proxy IP address.
2. $ _ SERVER ['HTTP _ CLIENT_IP ']; proxy IP address, which may exist and can be forged.
3. $ _ SERVER ['HTTP _ X_FORWARDED_FOR ']; the proxy of the user's IP address, which may exist and can be forged.
Server IP-related variables
1. $ SERVER_NAME, which must be obtained using the gethostbyname () function. This variable can be correctly displayed on both the server side and the client side.
2. $ HTTP_SERVER_VARS ["SERVER_ADDR"], test on the server: 127.0.0.1 (this is related to the BindAddress setting value in httpd. conf ). The test results on the client are correct.
3. $ _ SERVER ['local _ ADDR '] and $ HTTP_SERVER_VARS ['local _ ADDR']. No results were obtained during the test (PHP5 test environment ).
Obtain the complete IP class
[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,' <br/> ';
28. echo 'server IP is ', $ serverIp,' <br/> ';