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