php Get visitor IP address Rollup
In my time we need to get the user's real IP address, for example, logging, geographic positioning, the user information, site data analysis, and so on, actually get the IP address is very simple $_server[' remote_addr ' on it. Here we will give you a summary of several commonly used to obtain IP address methods.
Method 1:
?
1 2 |
$ip = $_server["REMOTE_ADDR"]; Echo $ip; |
Method 2:
The code is as follows:
$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;
Method 3:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function Getrealip () {$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 ). ", $ips [$i])) {$ip = $ips [$i]; break;}} Return ($ip $ip: $_server[' remote_addr ')); Echo Getrealip (); |
Method 4:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28-29 |
if ($HTTP _server_vars["Http_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_f or "); } elseif (getenv ("Http_client_ip")) {$ip = getenv ("Http_client_ip");} elseif (Getenv ("REMOTE_ADDR")) {$ip = getenv ("REM Ote_addr "); else {$ip = "Unknown";} Echo $ip; |
Method 5:
?
1 2 3 4 5 6 7 8 9 10 |
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; |
Method 6:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 |
Print "Your IP address is:"; if (!empty ($_server["Http_client_ip")) {$cip = $_server["Http_client_ip"]; ElseIf (!empty $_server[ Forwarded_for "])) {$cip = $_server[" Http_x_forwarded_for "]; ElseIf (!empty ($_server[" REMOTE_ADDR "])) {$cip = $_server ["REMOTE_ADDR"]; } else{$cip = "cannot be obtained!" "; } Print $cip; |
The above mentioned is the entire content of this article, I hope to learn PHP can help you.