Encapsulation class for PHP to obtain client and server IP addresses, and for php server ip address Encapsulation
This example describes the encapsulation class for PHP to obtain the IP addresses of clients and servers. We will share this with you for your reference. The details are as follows:
Client IP-related variables:
1. $ _ SERVER ['remote _ ADDR ']; Client IP address, which may be the user IP address or the proxy IP address.
2. $ _ SERVER ['HTTP _ CLIENT_IP ']; proxy IP address, which may exist and can be forged.
3. $ _ SERVER ['HTTP _ X_FORWARDED_FOR ']; the proxy of the user's IP address, which may exist and can be forged.
Variables related to server IP:
1. $ _ SERVER ["SERVER_NAME"], which must be obtained using the gethostbyname () function. This variable can be correctly displayed on both the server side and the client side.
2. $ _ SERVER ["SERVER_ADDR"], test on the SERVER: 127.0.0.1 (this is related to the BindAddress setting value in httpd. conf ). The test results on the client are correct.
Class:
class getIP{ function clientIP(){ $cIP = getenv('REMOTE_ADDR'); $cIP1 = getenv('HTTP_X_FORWARDED_FOR'); $cIP2 = getenv('HTTP_CLIENT_IP'); $cIP1 ? $cIP = $cIP1 : null; $cIP2 ? $cIP = $cIP2 : 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 />';