PHP code for displaying the Client IP address and Server IP Address

Source: Internet
Author: User

Let's see.Code:

Copy code The Code is as follows: Echo "(1) the IP address of the user who browses the current page is :";
Echo $ _ server ['remote _ ADDR '];
Echo "<br/> ";
Echo "(2) the IP address of the user browsing the current page is :";
Echo getenv ('remote _ ADDR ');
Echo "<br/> ";
Echo "the IP address of the host www.baidu.com is :";
Echo gethostbyname (<a href = "http://www.baidu.com"> www.baidu.com </a> );

The output result is:
(1) the IP address of the user browsing the current page is 127.0.0.1.
(2) the IP address of the user browsing the current page is 127.0.0.1.
The IP address of the host www.baidu.com is 61.135.169.105.
You can use either of the following methods to obtain the Client IP Address:
The first is to use:
? $ _ Server ['remote _ ADDR ']
It is browsing the IP address of the user on the current page. The output here is 127.0.0.1, because this is a local test and the output is my local loop address.
The two are used:
? Getenv ('remote _ ADDR ')
The getenv: gets the value of an environment variable (get the values of various environment variables) is used here. Return Value: returns the value of the environment variable varname, or false on an error (if it fails, false is returned ).
Obtain the IP address of the server:
? Gethostbyname (<a href = "http://www.baidu.com"> www.baidu.com </a>)
The gethostbyname: get the IP address corresponding to a given Internet host name is used here (the IP address is obtained by a given host name). The returned value is: returns the IP address of the Internet host specified by hostname or a string containing the unmodified hostname on failure (if failure, the original input character host name is returned ).
Note the last sentence here, that is, if it fails, it will be output as is, for example:
? Echo "invalid host iwilldown IP Address :";
Echo gethostbyname ("iwilldown ");
Output:
? Invalid host iwilldown IP Address: iwilldown
Of course, this is not an IP address ....

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.