PHP shows the code _php tips for client IP and server IP

Source: Internet
Author: User

Take a look at the code:

Copy Code code as follows:

echo "(1) The IP address of the user browsing 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 "Host www.baidu.com IP address is:";
Echo gethostbyname (<a href= "http://www.baidu.com" >www.baidu.com</A>);

Its output results are:
(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
There are two ways to get the IP address of a client:
The first one is to use:
? $_server[' REMOTE_ADDR ']
It is browsing the current page of the user's IP address, the output here is 127.0.0.1, because this is a local test, the output is my local loop address.
The first two are used:
? getenv (' REMOTE_ADDR ')
This uses the function getenv:gets the value of an environment variable (get the values of various environment variables), return value: Returns The value of the environment variable V Arname, or False on an error (return FALSE if failed).
About getting the server-side IP address:
? gethostbyname (<a href= "http://www.baidu.com" >www.baidu.com</A>)
Here you use the function gethostbyname:get the IP address corresponding to a given Internet host name (which is obtained by a given host name), return value: Returns The IP address of the Internet host specified by hostname or a string containing the unmodified hostname on failure (failed to return The input character host name as is).
Notice the last sentence here, that is to say, if it fails, it will output as it 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.