获取IP:
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']);
}
过滤IP:
function check_ip(){
$ALLOWED_IP=array('112.91.81.82','221.4.223.112','221.4.223.107','192.168.*.*','127.0.0.1');
$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;
}
}
补充点知识在这里:
连接数据库:
$host='221.2.56.125';
$db_user='user';
$db_pass='pwd';
$db_name='table'; $link=mysql_connect($host,$db_user,$db_pass) or die("can not connect database"); mysql_select_db($db_name,$link);
//mysql_query("SET names UTF8");
//header("Content-Type:text/html;charset=utf-8");