The examples in this article describe the encapsulation classes of PHP acquiring client and server-side IP. Share to everyone for your reference, specific as follows:
Client IP-related variables:
1. $_server[' REMOTE_ADDR ']; Client IP, there may be the user's IP, but also may be the agent's IP.
2. $_server[' Http_client_ip ']; Agent-side IP, may exist, can be forged.
3. $_server[' http_x_forwarded_for ']; The user is in which IP to use the agent that may exist and can be forged.
Server-side IP-related variables:
1. $_server["SERVER_NAME"], you need to use the function gethostbyname () to obtain. This variable is displayed correctly on both the server side and the client.
2. $_server["SERVER_ADDR"], testing on the server side: 127.0.0.1 (this is related to the set value of Bindaddress in httpd.conf). The test results are correct on the client.
The 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, ' <br/> ';
Echo ' Server IP is ', $serverIp, ' <br/> ';
More interested in PHP related content readers can view the site topics: "PHP Network Programming Skills Summary", "Php Curl Usage Summary", "PHP Socket Usage Summary", "PHP Regular Expression Usage summary", "PHP string (String) Usage Summary", " PHP array Operation techniques Encyclopedia, "PHP Mathematical Calculation Skills Summary", "PHP object-oriented Programming Introductory Course", "PHP Data structure and algorithm tutorial", "PHP Programming algorithm Summary" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design.