PHP防止注入安全コード

2713 ワード

簡単に述べます。    説明:    転送変数に不正な文字が含まれているかどうかを判断します。    ようですPOST、$uGET    機能:注入防止    **************************/      

<?php     

//      
$ArrFiltrate=array("'",";","union");     
// url,      
$StrGoUrl="";     
//      
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;     
  }     
}     

//      
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として保存します。      そして各phpファイルの前にincludeを追加します。すぐできます     方法2     

/*  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);     
}     
}     

/*   */    
//      
function get_int($number)     
{     
    return intval($number);     
}     
//      
function get_str($string)     
{     
    if (!get_magic_quotes_gpc()) {     
return addslashes($string);     
    }     
    return $string;     
}