日期:2014-05-17 浏览次数:20752 次
function ip_test($ip,$iprule){ $ipruleregexp=str_replace('.*','ph',$iprule); $ipruleregexp=preg_quote($ipruleregexp,'/'); $ipruleregexp=str_replace('ph','\.[0-9]{1,3}',$ipruleregexp); if(preg_match('/^'.$ipruleregexp.'$/',$ip)) return true; else return false; }
$curr_ip=$_SERVER['REMOTE_ADDR']; $white_list=array(...); //白名单规则 $test_success=false; foreach($white_list as $iprule){ if(ip_test($curr_ip,$iprule)){ $test_success=true; break; } } if(!$test_success) exit('IP not in white list');
$curr_ip=$_SERVER['REMOTE_ADDR']; $black_list=array(...); //黑名单规则 foreach($black_list as $iprule){ if(ip_test($curr_ip,$iprule)){ exit('IP in black list'); } }