发布:sunday01 来源:net 【大 中 小】
本文介绍一个,php获取客户端的IP地址的实例代码,有需要的朋友参考下吧。本文转自:
http://www.jbxue.com/article/9627.html
获取客户端IP地址的代码,如下:
复制代码代码示例:
<?php
//取得客户端IP的函数
function get_client_ip() {
$ip = $_SERVER['REMOTE_ADDR'];
if ( isset($_SERVER['HTTP_CLIENT_IP']) && preg_match(‘/^([0-9]{1,3}.){3}[0-9]{1,3}$/’, $_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all(‘#d{1,3}.d{1,3}.d{1,3}.d{1,3}#s’, $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
foreach ($matches[0] AS $xip) { // www.jbxue.com
if (!preg_match(‘#^(10|172.16|192.168).#’, $xip)) {
$ip = $xip;
break;
}
}
}
return $ip;
}
?>
//取得客户端IP的函数
function get_client_ip() {
$ip = $_SERVER['REMOTE_ADDR'];
if ( isset($_SERVER['HTTP_CLIENT_IP']) && preg_match(‘/^([0-9]{1,3}.){3}[0-9]{1,3}$/’, $_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all(‘#d{1,3}.d{1,3}.d{1,3}.d{1,3}#s’, $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
foreach ($matches[0] AS $xip) { // www.jbxue.com
if (!preg_match(‘#^(10|172.16|192.168).#’, $xip)) {
$ip = $xip;
break;
}
}
}
return $ip;
}
?>
代码说明:
判断顺序
1.$_SERVER['HTTP_CLIENT_IP']
2.$_SERVER['HTTP_X_FORWARDED_FOR']
3.$_SERVER['REMOTE_ADDR']