Get an IP address in PHP

Source: Internet
Author: User

function GetIP () {

if (!empty ($_server["Http_x_forwarded_for")) {
$cip = $_server["Http_x_forwarded_for"];
}
ElseIf (!empty ($_server["Http_client_ip")) {
$cip = $_server["Http_client_ip"];
}
ElseIf (!empty ($_server["REMOTE_ADDR")) {
$cip = $_server["REMOTE_ADDR"];
}
else{
$CIP = "Cannot get!" ";
}
return $CIP;
}
echo GetIP ();

-----------------------------------------------------------------

function GetIP () {if (Isset ($_server[' http_x_forwarded_for ')) {$realip = $_server[' http_x_forwarded_for '];} elseif (i Sset ($_server[' http_client_ip ')) {$realip = $_server[' http_client_ip '];} else {$realip = $_server[' remote_addr '];} RE Turn $realip; }

Use $_server["REMOTE_ADDR" in PHP to obtain the IP address of the client, but if the client is accessed using a proxy server, the

is the IP address of the proxy server, not the true client IP address. To get the real IP address of the client through the proxy server, you need to use

$_server["Http_x_forwarded_for"] to read .


However, it is important to note that not every proxy server can use $_server["http_x_forwarded_for"] to read the client's real IP, some use this

The IP of the proxy server is still read by the method.

It is also important to note that if the client is not accessed through a proxy server , the value taken with $_server["Http_x_forwarded_for"] will

is empty .

HTTP_CLIENT_IP is the HTTP header sent by the proxy server. If "Super Anonymous proxy", the value of none is returned. Similarly, REMOTE_ADDR will be replaced with the IP of this proxy server.

REMOTE_ADDR is the IP of your client when it "shakes hands" with your server. If you use anonymous proxy, REMOTE_ADDR displays the IP of the proxy server.

Get an IP address in PHP

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.