获取IP:
1 function getIP(){ 2 return isset($_SERVER['HTTP_X_FORWARDED_FOR'])? 3 $_SERVER['HTTP_X_FORWARDED_FOR']: 4 (isset($_SERVER['HTTP_CLIENT_IP'])? 5 $_SERVER['HTTP_CLIENT_IP']:$_ SERVER['REMOTE_ADDR']); 6 }
过滤IP:
1 function check_ip(){ 2 $ALLOWED_IP=array('112.91.81.82','221.4.223.112','221.4.223.107','192.168.*.*','127.0.0.1'); 3 $IP=getIP(); 4 $check_ip_arr= explode('.',$IP);//要检测的ip拆分成数组 5 #限制IP 6 if(!in_array($IP,$ALLOWED_IP)) { 7 foreach ($ALLOWED_IP as $val){ 8 if(strpos($val,'*')!==false){//发现有*号替代符 9 $arr=array();// 10 $arr=explode('.', $val); 11 $bl=true;//用于记录循环检测中是否有匹配成功的 12 for($i=0;$i<4;$i++){ 13 if($arr[$i]!='*'){//不等于* 就要进来检测,如果为*符号替代符就不检查 14 if($arr[$i]!=$check_ip_arr[$i]){ 15 $bl=false; 16 break;//终止检查本个ip 继续检查下一个ip 17 } 18 } 19 }//end for 20 if($bl){//如果是true则找到有一个匹配成功的就返回 21 return; 22 die; 23 } 24 } 25 }//end foreach 26 header('HTTP/1.1 403 Forbidden'); 27 echo "Access forbidden"; 28 die; 29 } 30 }
补充点知识在这里:
连接数据库:
1 $host='221.2.56.125'; 2 $db_user='user'; 3 $db_pass='pwd'; 4 $db_name='table'; 5 6 $link=mysql_connect($host,$db_user,$db_pass) or die("can not connect database"); 7 8 mysql_select_db($db_name,$link); 9 //mysql_query("SET names UTF8"); 10 //header("Content-Type:text/html;charset=utf-8");