REMOTE_ADDR can only get the visitors in the local connection setup IP, such as their own set up 10. In a certain university campus network 7.0.x.x XX, XXX series IP, and it is the function for LAN gateway IP address for export, if visitors use a proxy server, will not get the proxy server IP, but the real IP gateway for visitors. If apply this function to the limit of IP access web pages, even if others by restricted access section of the proxy server IP, also not be able to access this page.

the following provides a function:

code is 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 ");
the else
$IP="Unknow";

return $IP;
}

//method of use:
echo getIP ();

? >

the getenv (" REMOTE_ADDR ") is used to obtain the client's IP address, but if the client is using a proxy server to access, it is taken the IP address of the proxy server, rather than the real client IP address. You want to get the client's real through the proxy server IP address, then use the getenv (" HTTP_X_FORWARDED_FOR ") to read.

but if the client is not through a proxy server to access, then use the getenv (" HTTP_X_FORWARDED_FOR ") to the value will be empty.

code is as follows:



else if (getenv (" HTTP_X_FORWARDED_FOR "))
$IP=getenv (" HTTP_X_FORWARDED_FOR ");

said if the getenv (" HTTP_X_FORWARDED_FOR ") to the value of the existence is not null (that is, under the condition of the client using a proxy server), the variable $IP is equal to the getenv (" HTTP_X_FORWARDED_FOR ") to get the real IP values.

if the above else if (getenv (" HTTP_X_FORWARDED_FOR ")), the value is empty (i.e., without using a proxy server), and will not perform the following $IP=getenv (" HTTP_X_FORWARDED_FOR "); This line statements.

this case has confirmed that the client does not use a proxy server, thus by

code is as follows:


else if (getenv (" REMOTE_ADDR "))
$IP=getenv (" REMOTE_ADDR ");

the two line for the client's IP address is the IP address of the real.


This concludes the body part