最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP防注入安全代碼

 更新時(shí)間:2008年04月09日 23:05:01   作者:  
判斷傳遞的變量中是否含有非法字符我們把以下代碼放到一個(gè)公共的文件里,比如security.inc.php里面,每個(gè)文件里都include一下這個(gè)文件,那么就能夠給任何一個(gè)程序進(jìn)行提交的所有變量進(jìn)行過濾了,就達(dá)到了我們一勞永逸的效果。
簡(jiǎn)述:/*************************   
說明:   
判斷傳遞的變量中是否含有非法字符   
如$_POST、$_GET   
功能:防注入   
**************************/      

復(fù)制代碼 代碼如下:

<?php     

//要過濾的非法字符     
$ArrFiltrate=array("'",";","union");     
//出錯(cuò)后要跳轉(zhuǎn)的url,不填則默認(rèn)前一頁(yè)     
$StrGoUrl="";     
//是否存在數(shù)組中的值     
function FunStringExist($StrFiltrate,$ArrFiltrate){     
foreach ($ArrFiltrate as $key=>$value){     
  if (eregi($value,$StrFiltrate)){     
    return true;     
  }     
}     
return false;     
}     

//合并$_POST 和 $_GET     
if(function_exists(array_merge)){     
  $ArrPostAndGet=array_merge($HTTP_POST_VARS,$HTTP_GET_VARS);     
}else{     
  foreach($HTTP_POST_VARS as $key=>$value){     
    $ArrPostAndGet[]=$value;     
  }     
  foreach($HTTP_GET_VARS as $key=>$value){     
    $ArrPostAndGet[]=$value;     
  }     
}     

//驗(yàn)證開始     
foreach($ArrPostAndGet as $key=>$value){     
  if (FunStringExist($value,$ArrFiltrate)){     
    echo "<script language=\"javascript\">alert(\"非法字符\");</script>";     
    if (emptyempty($StrGoUrl)){     
    echo "<script language=\"javascript\">history.go(-1);</script>";     
    }else{     
    echo "<script language=\"javascript\">window.location=\"".$StrGoUrl."\";</script>";     
    }     
    exit;     
  }     
}     
?> 
    

保存為checkpostandget.php     
然后在每個(gè)php文件前加include(“checkpostandget.php“);即可     

方法2     

復(fù)制代碼 代碼如下:

/* 過濾所有GET過來變量 */    
foreach ($_GET as $get_key=>$get_var)     
{     
if (is_numeric($get_var)) {     
  $get[strtolower($get_key)] = get_int($get_var);     
} else {     
  $get[strtolower($get_key)] = get_str($get_var);     
}     
}     

/* 過濾所有POST過來的變量 */    
foreach ($_POST as $post_key=>$post_var)     
{     
if (is_numeric($post_var)) {     
  $post[strtolower($post_key)] = get_int($post_var);     
} else {     
  $post[strtolower($post_key)] = get_str($post_var);     
}     
}     

/* 過濾函數(shù) */    
//整型過濾函數(shù)     
function get_int($number)     
{     
    return intval($number);     
}     
//字符串型過濾函數(shù)     
function get_str($string)     
{     
    if (!get_magic_quotes_gpc()) {     
return addslashes($string);     
    }     
    return $string;     
}
       

相關(guān)文章

最新評(píng)論

监利县| 罗江县| 成都市| 海安县| 双柏县| 松桃| 汕尾市| 如皋市| 雷山县| 富川| 巴林右旗| 九台市| 永登县| 汶川县| 双辽市| 淄博市| 崇信县| 临沂市| 威信县| 荃湾区| 夏河县| 尼木县| 荥阳市| 武宁县| 双城市| 芷江| 秭归县| 惠州市| 富顺县| 阳江市| 泾阳县| 朝阳区| 临洮县| 马鞍山市| 历史| 华池县| 阿拉尔市| 泸西县| 长海县| 平山县| 麻江县|