PHP gets client IP and server-side IP methods
Client IP-related variables
1. $_server[' REMOTE_ADDR '; Client IP, which may be the IP of the user or the IP of the proxy.
2. $_server[' http_client_ip '; Proxy-side IP, may exist, can be forged.
3. $_server[' http_x_forwarded_for '; The user is the agent in which IP is used, which may exist and can be forged.
Server-side IP-related variables
1. $_server["SERVER_NAME"), need to use function gethostbyname () to obtain. This variable can be displayed correctly both on the server side and on the client.
2. $_server["Server_addr"], on the server side test: 127.0.0.1 (this is related to the Bindaddress setting value in httpd.conf). The test results on the client are correct.
Classes are as follows:
Class Getip{function ClientIP () {$cIP = getenv (' remote_addr '), $cIP 1 = getenv (' http_x_forwarded_for '); $cIP 2 = getenv (' Http_client_ip '); $cIP 1? $cIP = $cIP 1:null; $cIP 2? $cIP = $cIP 2:null;return $cIP; }function ServerIP () {return gethostbyname ($_server["SERVER_NAME"]);}} $getIP = new GetIP (); $clientIp = Getip::clientip (); $serverIp = Getip::serverip (); Echo ' Client IP is ', $clientIp, '
'; Echo ' Server IP is ', $serverIp, '
'; function GetIP () {$ip = $_server[' remote_addr '];if (!empty ($_server[' http_client_ip ')) {$ip = $_server[' http_client _ip ']; } elseif (!empty ($_server[' http_x_forwarded_for ')) {$ip = $_server[' http_x_forwarded_for ']; }return $ip;} function getip__ () {if (getenv ("Http_client_ip") && strcasecmp (getenv ("Http_client_ip"), "unknown") $ip = Getenv ("Http_client_ip"), Else if (getenv ("Http_x_forwarded_for") && strcasecmp (getenv ("Http_x_forwarded_ For ")," Unknown ")) $ip = getenv (" Http_x_forwarded_for "), Else if (getenv (" REMOTE_ADDR ") && strcasecmp (getenv (" Remote_addr ")," Unknown ")) $ip = getenv (" REMOTE_ADDR "), Else if (isset ($_server[' remote_addr ')) && $_server[' Remote_addr '] && strcasecmp ($_server[' remote_addr '), "unknown") $ip = $_server[' remote_addr '];else$ip = " Unknown "; return ($IP);} echo GetIP ();