欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP防注入安全代碼

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

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

<?php     

//要過濾的非法字符     
$ArrFiltrate=array("'",";","union");     
//出錯(cuò)后要跳轉(zhuǎn)的url,不填則默認(rèn)前一頁     
$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)論