PHP access to client and server-side IP encapsulation class _php tips

Source: Internet
Author: User
Tags php programming php regular expression

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.

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.