PHP shows code _php for client IP and server IP tutorial

Source: Internet
Author: User
Take a look at the code:
Copy CodeThe code is as follows:
echo "(1) The IP address of the user browsing the current page is:";
echo $_server[' REMOTE_ADDR '];
echo "
";
echo "(2) the IP address of the user browsing the current page is:";
echo getenv (' remote_addr ');
echo "
";
echo "host www.baidu.com's IP address is:";
Echo gethostbyname (www.baidu.com);

It outputs the result:
(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 the client:
The first one is to use:
? $_server[' REMOTE_ADDR ')
It is browsing the current page user's IP address, here the output is 127.0.0.1, because this is the local test, the output is my local loop address.
The first two are used:
? getenv (' REMOTE_ADDR ')
The function is used here 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 (returns False if failed).
About getting the server-side IP address:
? gethostbyname (www.baidu.com)
The function is used here Gethostbyname:get the IP address corresponding to a given Internet host name (with the given host name to get its IP addresses), return value: Returns The IP address of the Internet host specified by hostname or a string containing the unmodified hostname on failure (failed return The host name of the input character returned as it is.
Notice the last sentence here, that is, if it fails, it will output as-is, for example:
echo "Invalid host Iwilldown IP address is:";
Echo gethostbyname ("Iwilldown");
Output:
The IP address of the invalid host Iwilldown is: Iwilldown
Of course, this is not an IP address ....

http://www.bkjia.com/PHPjc/322537.html www.bkjia.com true http://www.bkjia.com/PHPjc/322537.html techarticle take a look at the code: Copy the Code as follows: Echo "(1) The IP address of the user browsing the current page is:"; Echo $_server[' remote_addr ']; echo "BR/"; echo "(2) User browsing the current page ...

  • 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.