php专区

 首页 > php专区 > PHP应用 > php函数大全 > 禁止IP的函数

禁止IP的函数

分享到:
【字体:
导读:
          function check_ip($range,$ip=& 39;& 39;) {if($ip == & 39;& 39;) $ip = getenv("REMOTE_ADDR");if ($ip == "127 0 0 1") return 1;$result = 1;if (ereg("([0-9] ) ([0-9] ) ([0-9] ) ([0-

 

function check_ip($range,$ip='') {
if($ip == '') $ip = getenv("REMOTE_ADDR");
if ($ip == "127.0.0.1") return 1;
$result = 1;
if (ereg("([0-9] ).([0-9] ).([0-9] ).([0-9] )/([0-9] )",$range,$regs)) {
$ipl = ip2long($ip);
$rangel = ip2long($regs[1] . "." . $regs[2] . "." . $regs[3] . "." . $regs[4]);

$maskl = 0;

for ($i = 0; $i< 31; $i ) {
if ($i < $regs[5]-1) {
$maskl = $maskl pow(2,(30-$i));
}
}

if (($maskl & $rangel) == ($maskl & $ipl)) {
return 1;
} else {
return 0;
}
} else {

$maskocts = split(".",$range);
$ipocts = split(".",$ip);
for ($i=0; $i<4; $i ) {
if (ereg("[([0-9] )-([0-9] )]",$maskocts[$i],$regs)) {
if ( ($ipocts[$i] > $regs[2]) || ($ipocts[$i] < $regs[1])) {
$result = 0;
}
}
else
{
if ($maskocts[$i] <> $ipocts[$i]) {
$result = 0;
}
}
}
}
return $result;
}

function auth_ip(){
$result = 0;
$null_check = 1;

$path = "ip.cf"; //ip限制配置文件,每行的格式为
/*
xxx.xxx.xxx.xxx 比如 127.0.0.2
xxx.xxx.xxx.[yyy-zzz] 比如 1270.0.0.[2-23]
xxx.xxx.xxx.xxx/nn 比如 127.0.0.0/24
*/
$fg=@fopen($path,"r");

while($line=@fgets($fg,1024)){
$line = trim($line);
$line=ereg_replace("#.*","",$line);
if ($line != ""){
$null_check = 0;
if (check_ip($line)) {
fclose($fg);
return 1;
}
}
}
@fclose($fg);
if ($null_check == 1) return 1;
return $result;
}


?>

分享到:
用PHP来制作评论系统
  我们在实际做的过程中很是简单的,希望大家好好研究一下,给补充多点功能。本程序须在PHP and mySQL的环境下运行。有三个文件:comments.php, 是用来显示[评论的, commentadd.php, 用来处理评论内容的, and commentform.html 通过FROM来提交评论。   1.首先建立一个数据库,假如已经建立则建立一个符合条件的表: ...
判断中文的函数
  $a=&#039;english&#039;; $b=&#039;中文&#039;; $c=&#039;english&中文&#039;; function chkGB($Str) { $StrLen = strlen($Str); $Length = 1; for($i = 0;$i < $StrLen;$i ) { $TmpStr = ord(substr($Str,$i,1)); $TmpStr2 = ord(substr($Str,$i 1,1)); if(($TmpStr =247) && ($TmpStr2 =247)) { $LegalFl...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……