In php, a simple global variable for obtaining IP addresses is provided. Many beginners use this variable to obtain IP addresses. However, we generally use this variable to meet the requirements, however, this function does not work for high precision. In php, a simple global variable for obtaining IP addresses is provided. Many beginners use this variable to obtain IP addresses. However, we generally use this variable to meet the requirements, however, this function does not work for high precision.
Script ec (2); script
This is the simplest method. There is no way to enable transparent proxy. If Intranet access cannot read the correct Internet IP address, it is effort-saving:
The Code is as follows: |
|
$ Ip = $ _ SERVER ["REMOTE_ADDR"]; |
Done ~
The above method is used to obtain the IP address of the client, but if the client is accessed by the proxy server, the obtained IP address is the IP address of the proxy server, not the real IP address of the client.
To obtain the real IP address of the client through the proxy server, use getenv ("HTTP_X_FORWARDED_FOR") to read.
However, if the client is not accessed through the proxy server, the value obtained using getenv ("HTTP_X_FORWARDED_FOR") will be null.
The Code is as follows: |
|
Else if (getenv ("HTTP_X_FORWARDED_FOR ")) $ Ip = getenv ("HTTP_X_FORWARDED_FOR "); |
Indicates that if the value obtained by getenv ("HTTP_X_FORWARDED_FOR") is not null (that is, when the client uses the proxy server), the variable $ ip is equal to getenv ("HTTP_X_FORWARDED_FOR ") the actual IP address.
If the value obtained by the preceding else if (getenv ("HTTP_X_FORWARDED_FOR") is null (that is, no proxy server is used ), the following $ ip = getenv ("HTTP_X_FORWARDED_FOR") is not executed.
In this case, it has been confirmed that the client does not use the proxy server
The Code is as follows: |
|
Else if (getenv ("REMOTE_ADDR ")) $ Ip = getenv ("REMOTE_ADDR "); |
These two statements obtain the client's IP address, which is also a real IP address.
The Code is as follows: |
|
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 "); } Www.111cn.net } Return $ realip; } |
In addition, if we want to obtain more accurate third-party services, it is a good method.
The Code is as follows: |
|
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]; } |
In this way, you can determine the IP address without managing the proxy.