How PHP Gets the client IP address

Source: Internet
Author: User
PHP to obtain the client IP address several ways to read PHP to obtain the client IP address of several methods, <?php $iipp =$_server["REMOTE_ADDR"];  
Echo $iipp; ?> <?php $user _ip = ($_server["Http_via"])?  
$_server["Http_x_forwarded_for"]: $_server["REMOTE_ADDR"]; $user _ip = ($user _ip)?  
$user _ip: $_server["REMOTE_ADDR"];  
echo $user _ip;  
?> <?php function Get_real_ip () {$ip =false;  
if (!empty ($_server["HTTP_CLIENT_IP"])) {$ip = $_server["Http_client_ip"];  
} if (!empty ($_server[' http_x_forwarded_for ')) {$ips = Explode (",", $_server[' http_x_forwarded_for ']);  
if ($IP) {array_unshift ($ips, $ip); $ip = FALSE;}  
for ($i = 0; $i < count ($ips); $i + +) {if (!eregi (10|172\.16|192\.168) \., $ips [$i]) {$ip = $ips [$i];  
Break  
Return ($ip $ip: $_server[' remote_addr '));  
Echo Get_real_ip (); ?> <?php//php an algorithm for obtaining IP//url:http://www.bianceng.cn/webkf/php/201410/45962.htm if ($HTTP _server_vars["HTT" P_x_forwarded_for "]) {$ip = $HTTP _server_vars["Http_x_forwarded_for"];  
} elseif ($HTTP _server_vars["Http_client_ip"]) {$ip = $HTTP _server_vars["Http_client_ip"];  
} elseif ($HTTP _server_vars["REMOTE_ADDR"]) {$ip = $HTTP _server_vars["REMOTE_ADDR"];  
} elseif (getenv ("Http_x_forwarded_for")) {$ip = getenv ("Http_x_forwarded_for");  
} elseif (getenv ("Http_client_ip")) {$ip = getenv ("Http_client_ip");  
} elseif (getenv ("REMOTE_ADDR")) {$ip = getenv ("REMOTE_ADDR");  
else {$ip = "Unknown";  
echo "Your IP:". $ip;  
?> <?php if (getenv (' http_client_ip ')) {$onlineip = getenv (' http_client_ip ');  
} elseif (getenv (' http_x_forwarded_for ')) {$onlineip = getenv (' http_x_forwarded_for ');  
} elseif (getenv (' remote_addr ')) {$onlineip = getenv (' remote_addr ');  
else {$onlineip = $HTTP _server_vars[' remote_addr '];  
Echo $onlineip; ?> <?php function GetIP ()/* Get client ip*/{if (@$_server["Http_x_forwarded_for"]) $ip = $_server["Http_x_forwarded_for"];  
else if (@$_server["Http_client_ip"]) $ip = $_server["Http_client_ip"];  
else if (@$_server["REMOTE_ADDR"]) $ip = $_server["REMOTE_ADDR"];  
else if (@getenv ("Http_x_forwarded_for")) $ip = getenv ("Http_x_forwarded_for");  
else if (@getenv ("Http_client_ip")) $ip = getenv ("Http_client_ip");  
else if (@getenv ("REMOTE_ADDR")) $ip = getenv ("REMOTE_ADDR");  
else $ip = "Unknown";  
return $IP;  
echo GetIP ();  
      
  ?> 1: Look at the Code: 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 IP address is:";  
      
Echo gethostbyname (www.baidu.com);  2: Its output 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 host www.baidu.com  
      
  The IP address is: 61.135.169.105 3: About getting the client's IP address, there are two ways:The first 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 ') here uses the function getenv:gets the value of an environment variable (get all kinds of loops  
      
Value of the boundary variable, return value: Returns The value of the environment variable varname, or False on ' an error (return FALSE if failed). 4: About getting the server-side IP address: gethostbyname (www.baidu.com) here uses the function gethostbyname:get the IP addressing Correspon Ding to a given Internet host name (which obtains its IP address via a given host name), return value: Returns The IP addresses of the Internet host specified by Ho  
      
Stname or a string containing the unmodified hostname on failure (returns the input character host name as it would have failed).    
5: Note the last sentence here, that is to say, if it fails, it will 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 ... ~ ~ ~ ~ ~ ~ ~ ~ ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.