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