REMOTE_ADDR can only obtain IP settings that are set in the visitor's local connection, such as 10 of its own on a university campus network. X.xxx. XXX Series IP, and this function to get the IP address of the LAN gateway exit, if the visitor uses proxy server, will not obtain the proxy server's IP, but gets the visitor gateway real IP. If you apply this function to a Web page that is restricted to IP access, someone else cannot access the page even though the proxy server in the IP Access section is restricted.
A function is provided below:
Copy Code code as follows:
<?php
Define a function GetIP ()
function GetIP ()
{
Global $ip;
if (getenv ("Http_client_ip"))
$ip = getenv ("Http_client_ip");
else if (getenv ("Http_x_forwarded_for"))
$ip = getenv ("Http_x_forwarded_for");
else if (getenv ("REMOTE_ADDR"))
$ip = getenv ("REMOTE_ADDR");
Else
$ip = "Unknow";
return $IP;
}
How to use:
echo GetIP ();
?>
Getenv ("REMOTE_ADDR") 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 a client's real IP address through a proxy server, use getenv ("Http_x_forwarded_for") to read it.
However, if the client is not accessed through a proxy server, the value taken with getenv ("Http_x_forwarded_for") will be empty.
Copy Code code as follows:
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 is using a proxy server), then the variable $ip equals the true IP value that getenv ("Http_x_forwarded_for") takes.
If the above else if (getenv ("http_x_forwarded_for")) gets a value that is null (that is, no proxy server is used), the following $ip = getenv ("Http_x_forwarded_for") is not executed;
In this case, you have confirmed that the client is not using a proxy server, thereby passing
Copy Code code as follows:
else if (getenv ("REMOTE_ADDR"))
$ip = getenv ("REMOTE_ADDR");
These two lines of statements obtain the IP address of the client and also the true IP address.