Get the server IP in PHP CLI mode

Source: Internet
Author: User
Tags php cli

(not organized ...)

(1) Get the server IP 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) Get client IP and server-side IP in PHP CGI mode

[PHP]

[PHP]
PHP Gets the client and server IP address functions in the following notation:
[Code=php width=600px]/**
* Get 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;
}
/**
* Get server-side 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, which may be the IP of the user or the IP of the proxy.

2. $_server[' http_client_ip '; Proxy-side IP, may exist, can be forged.

3. $_server[' http_x_forwarded_for '; The user is the agent in which IP is used, which may exist and can be forged.

Server-side IP-related variables
1. $SERVER _name, it needs to be obtained using function gethostbyname (). This variable can be displayed correctly both on the server side and on the client.

2. $HTTP _server_vars["Server_addr"], on the server side test: 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 '], $HTTP _server_vars[' local_addr '), no results were obtained in the test (test environment PHP5).
Complete access to IP class
[Code=php width=600px]01./**
* Get Client/server IP
03. *
* @author Yaron (http://yaron.org.cn)
* @version 0.1
. * @package
07. */
08.
09.class getip{
function ClientIP () {
$cIP = getenv (' remote_addr ');
$cIP 1 = getenv (' http_x_forwarded_for ');
$cIP 2 = getenv (' http_client_ip ');
$cIP 1? $cIP = $cIP 1:null;
$cIP 2? $cIP = $cIP 2:null;
. return $cIP;
17.}
function ServerIP () {
. return gethostbyname ($_server_name);
20.}
21.}
22.
$getIP = new GetIP ();
$clientIp = Getip::clientip ();
$serverIp = Getip::serverip ();
26.
27.echo ' Client IP is ', $clientIp, ' <br/> ';
28.echo ' Server IP is ', $serverIp, ' <br/> ';

PHP Gets the client and server IP address functions in the following notation:
[Code=php width=600px]/**
* Get 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;
}
/**
* Get server-side 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, which may be the IP of the user or the IP of the proxy.

2. $_server[' http_client_ip '; Proxy-side IP, may exist, can be forged.

3. $_server[' http_x_forwarded_for '; The user is the agent in which IP is used, which may exist and can be forged.

Server-side IP-related variables
1. $SERVER _name, it needs to be obtained using function gethostbyname (). This variable can be displayed correctly both on the server side and on the client.

2. $HTTP _server_vars["Server_addr"], on the server side test: 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 '], $HTTP _server_vars[' local_addr '), no results were obtained in the test (test environment PHP5).
Complete access to IP class
[Code=php width=600px]01./**
* Get Client/server IP
03. *
* @author Yaron (http://yaron.org.cn)
* @version 0.1
. * @package
07. */
08.
09.class getip{
function ClientIP () {
$cIP = getenv (' remote_addr ');
$cIP 1 = getenv (' http_x_forwarded_for ');
$cIP 2 = getenv (' http_client_ip ');
$cIP 1? $cIP = $cIP 1:null;
$cIP 2? $cIP = $cIP 2:null;
. return $cIP;
17.}
function ServerIP () {
. return gethostbyname ($_server_name);
20.}
21.}
22.
$getIP = new GetIP ();
$clientIp = Getip::clientip ();
$serverIp = Getip::serverip ();
26.
27.echo ' Client IP is ', $clientIp, ' <br/> ';
28.echo ' Server IP is ', $serverIp, ' <br/> ';

Get the server IP in PHP CLI mode

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.