php获取客户端IP地址的几种方法
php获取客户端IP地址的几种方法
PHP Code one:1 //php获取ip的算法
2 $iipp=$_SERVER["REMOTE_ADDR"];
3 echo $iipp;
2 $iipp=$_SERVER["REMOTE_ADDR"];
3 echo $iipp;
PHP Code two:
1 //php获取ip的算法
2 $user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
3 $user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];
4
5 echo $user_IP;
2 $user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
3 $user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];
4
5 echo $user_IP;
1 function get_real_ip(){
2 $ip=false;
3 if(!empty($_SERVER["HTTP_CLIENT_IP"])){
4 $ip = $_SERVER["HTTP_CLIENT_IP"];
5 }
6 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
7 $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
8 if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
9 for ($i = 0; $i < count($ips); $i++) {
10 if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
11 $ip = $ips[$i];
12 break;
13 }
14 }
15 }
16 return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
17 }
18
19 echo get_real_ip();
2 $ip=false;
3 if(!empty($_SERVER["HTTP_CLIENT_IP"])){
4 $ip = $_SERVER["HTTP_CLIENT_IP"];
5 }
6 if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
7 $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
8 if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
9 for ($i = 0; $i < count($ips); $i++) {
10 if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
11 $ip = $ips[$i];
12 break;
13 }
14 }
15 }
16 return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
17 }
18
19 echo get_real_ip();
PHP Code three:
1 //php获取ip的算法
2 if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])
3 {
4 $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
5 }
6 elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
7 {
8 $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
9 }
10 elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])
11 {
12 $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
13 }
14 elseif (getenv("HTTP_X_FORWARDED_FOR"))
15 {
16 $ip = getenv("HTTP_X_FORWARDED_FOR");
17 }
18 elseif (getenv("HTTP_CLIENT_IP"))
19 {
20 $ip = getenv("HTTP_CLIENT_IP");
21 }
22 elseif (getenv("REMOTE_ADDR"))
23 {
24 $ip = getenv("REMOTE_ADDR");
25 }
26 else
27 {
28 $ip = "Unknown";
29 }
30 echo "你的IP:".$ip ;
2 if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])
3 {
4 $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
5 }
6 elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
7 {
8 $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
9 }
10 elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])
11 {
12 $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
13 }
14 elseif (getenv("HTTP_X_FORWARDED_FOR"))
15 {
16 $ip = getenv("HTTP_X_FORWARDED_FOR");
17 }
18 elseif (getenv("HTTP_CLIENT_IP"))
19 {
20 $ip = getenv("HTTP_CLIENT_IP");
21 }
22 elseif (getenv("REMOTE_ADDR"))
23 {
24 $ip = getenv("REMOTE_ADDR");
25 }
26 else
27 {
28 $ip = "Unknown";
29 }
30 echo "你的IP:".$ip ;
PHP Code four:
1 //php获取ip的算法
2 if(getenv('HTTP_CLIENT_IP')) {
3 $onlineip = getenv('HTTP_CLIENT_IP');
4 } elseif(getenv('HTTP_X_FORWARDED_FOR')) {
5 $onlineip = getenv('HTTP_X_FORWARDED_FOR');
6 } elseif(getenv('REMOTE_ADDR')) {
7 $onlineip = getenv('REMOTE_ADDR');
8 } else {
9 $onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
10 }
11 echo $onlineip;
2 if(getenv('HTTP_CLIENT_IP')) {
3 $onlineip = getenv('HTTP_CLIENT_IP');
4 } elseif(getenv('HTTP_X_FORWARDED_FOR')) {
5 $onlineip = getenv('HTTP_X_FORWARDED_FOR');
6 } elseif(getenv('REMOTE_ADDR')) {
7 $onlineip = getenv('REMOTE_ADDR');
8 } else {
9 $onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
10 }
11 echo $onlineip;