Ways to get visitors ' IPs in PHP

Source: Internet
Author: User

In PHP comes with a very simple to get the IP address of the global variable, many beginners have access to the IP used it, but for these our general usage is satisfied, but for the requirement of high precision this function is not possible.

This is the simplest way to open a transparent proxy and the like is no way, if the intranet access can not read the correct external network IP, but it is very labor-saving is:

The code is as follows Copy Code

$ip = $_server["REMOTE_ADDR"];

Fix It ~

The above method is used to obtain the IP address of the client, but if the client is accessed using a proxy server, then the IP address of the proxy server is taken, not the real client IP address

To obtain the real IP address of the client through the proxy server, it is necessary to use getenv ("Http_x_forwarded_for") to read.
However, if the client is not accessed through a proxy server, the value taken with getenv ("Http_x_forwarded_for") will be empty.

The code is as follows Copy Code

else if (getenv ("Http_x_forwarded_for"))
$ip = getenv ("Http_x_forwarded_for");

Indicates that if getenv ("Http_x_forwarded_for") takes a value that is not empty (that is, if the client uses a proxy server), the variable $ip equals the real IP value taken by getenv ("Http_x_forwarded_for").
The following $ip = getenv ("http_x_forwarded_for") will not be performed if the else if (getenv ("Http_x_forwarded_for") above has a null value (that is, no proxy server is used);
In this case, it has been confirmed that the client is not using a proxy server,

The code is as follows Copy Code
else if (getenv ("REMOTE_ADDR"))
$ip = getenv ("REMOTE_ADDR");

These two lines of statements get the IP address of the client as well as the real IP address, based on experience

The code is as follows Copy Code

function GetIP ()
{
Static $realip;
if (Isset ($_server)) {
if (Isset ($_server["Http_x_forwarded_for")) {
$realip = $_server["Http_x_forwarded_for"];
} else if (Isset ($_server["Http_client_ip")) {
$realip = $_server["Http_client_ip"];
} else {
$realip = $_server["REMOTE_ADDR"];
}
} else {
if (getenv ("Http_x_forwarded_for")) {
$realip = getenv ("Http_x_forwarded_for");
} else if (getenv ("Http_client_ip")) {
$realip = getenv ("Http_client_ip");
} else {
$realip = getenv ("REMOTE_ADDR");
}
}
return $realip;
}


In addition, if we want to get more accurate use of third parties is a good way.

The code is as follows Copy Code

function Get_onlineip () {
$ch = Curl_init (' http://www.ip138.com/ip2city.asp ');
curl_setopt ($ch, Curlopt_returntransfer, true);
$a = curl_exec ($ch);
Preg_match ('/[(. *)]/', $a, $ip);
return $IP [1];
}

This does not manage the agent or anything can determine the IP address OH

Ways to get visitors ' IPs in PHP

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.