/**
* 检测访问的ip是否为规定的允许的ip
* Enter description here ...
*/
function check_ip(){
$ALLOWED_IP=array('192.168.2.*','127.0.0.1','192.168.2.49');
$IP=getIP();
$check_ip_arr= explode('.',$IP);//要检测的ip拆分成数组
#限制IP
if(!in_array($IP,$ALLOWED_IP)) {
foreach ($ALLOWED_IP as $val){
if(strpos($val,'*')!==false){//发现有*号替代符
$arr=array();//
$arr=explode('.', $val);
$bl=true;//用于记录循环检测中是否有匹配成功的
for($i=0;$i<4;$i++){
if($arr[$i]!='*'){//不等于* 就要进来检测,如果为*符号替代符就不检查
if($arr[$i]!=$check_ip_arr[$i]){
$bl=false;
break;//终止检查本个ip 继续检查下一个ip
}
}
}//end for
if($bl){//如果是true则找到有一个匹配成功的就返回
return;
die;
}
}
}//end foreach
header('HTTP/1.1 403 Forbidden');
echo "Access forbidden";
die;
}
}
function getIP() {
return isset($_SERVER["HTTP_X_FORWARDED_FOR"])?$_SERVER["HTTP_X_FORWARDED_FOR"]
:(isset($_SERVER["HTTP_CLIENT_IP"])?$_SERVER["HTTP_CLIENT_IP"]
:$_SERVER["REMOTE_ADDR"]);
}
以下为ASP版:
<%
'受屏蔽IP地址(段)集合,星号为通配符,通常保存于配置文件中。
Const BadIPGroup = "192.168.1.*|202.68.*.*|*.12.55.34|185.*.96.24|127.*.0.1|192.168.0.1"
If IsForbidIP(BadIPGroup) = True Then
Response.Write(GetIP &"IP地址禁止访问")
Response.End()
End If '参数vBadIP:要屏蔽的IP段,IP地址集合,用|符号分隔多个IP地址(段)
'返回Bool:True用户IP在被屏蔽范围,False 反之
Function IsForbidIP(vBadIP)
Dim counter, arrIPPart, arrBadIP, arrBadIPPart, i, j
arrBadIP = Split(vBadIP, "|")
arrIPPart = Split(GetIP(), ".")
For i = To UBound(arrBadIP)
counter =
arrBadIPPart = Split(arrBadIP(i), ".")
For j = To UBound(arrIPPart)
If(arrBadIPPart(j)) = "*" or Cstr(arrIPPart(j)) = Cstr(arrBadIPPart(j)) Then
counter = counter +
End If
Next
If counter = Then
IsForbidIP = True
Exit Function
End If
Next
IsForbidIP = False
End Function ''返回客户IP地址
Function GetIP()
Dim IP
IP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If IP = "" Then IP = Request.ServerVariables("REMOTE_ADDR")
GetIP = IP
End Function
%>