Encapsulation class for PHP to obtain client and server IP addresses, and for php server ip address Encapsulation

Source: Internet
Author: User

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 />';

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.