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