PHP shows the code for client IP and server IP

  • 2020-03-31 21:09:59
  • OfStack

Take a look at the code:

 
echo "(1) For the user browsing the current page  IP  The address is: "; 
echo $_SERVER['REMOTE_ADDR']; 
echo "<br />"; 
echo "(2) For the user browsing the current page  IP  The address is: "; 
echo getenv('REMOTE_ADDR'); 
echo "<br />"; 
echo " The host  www.baidu.com  the  IP  The address is: "; 
echo gethostbyname(<A href="http://www.baidu.com">www.baidu.com</A>); 

Its 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
There are two ways to get the client's IP address:
The first is to use:
? $_SERVER [' REMOTE_ADDR]
It is browsing the IP address of the current page user, and the output here is 127.0.0.1, because this is a local test, the output is my local loop address.
The second one USES:
? The getenv (" REMOTE_ADDR ")
The function getenv: Gets the value of an environment variable, Returns the value of the environment variable varname, or FALSE on an error.
About getting the IP address on the server side:
? Gethostbyname (< A href = "http://www.baidu.com" > www.baidu.com< / A>)
Gethostbyname: Get the IP address corresponding to a given Internet host name Returns the IP address of the Internet host specified by hostname or a string containing the unmodified hostname on failure.
Note the last sentence here, that is, if it fails, it will output as is, for example:
? Echo "the IP address of invalid host iwilldown is: ";
Echo gethostbyname (" iwilldown ");
Output:
? The IP address of the invalid host iwilldown is: iwilldown
Of course, this is not an IP address... .


Related articles: