How can I use PHP to obtain the client IP address and the server (host) IP address? For more information, see. Let's look at the 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"
";
Echo "(2) the IP address of the user browsing the current page is :";
Echo getenv ('remote _ ADDR ');
Echo"
";
Echo "the IP address of the host www.baidu.com is :";
Echo gethostbyname (www.baidu.com );
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 (www.baidu.com)
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 ....