Obtain the server ip address in phpcli mode

Source: Internet
Author: User
Tags php cli
(1) obtain the server ip address in phpcli mode [php] functiongetServerIp () {$ ss = exec (& amp; #39;/sbin/ifconfigeth0 | sed-n \ & amp; #39; s/^ *. * addr: \ ([0-9.] \\{ 7 ,\\}\\). * $/\ 1/p \ & amp; #39;

(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 ,'
';
28. echo 'server IP is ', $ serverIp ,'
';

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 ,'
';
28. echo 'server IP is ', $ serverIp ,'
';

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.