phpコミットデータの検証

8442 ワード

<?php

   //security.php

/**

 * @author zhengwei

 * @copyright 2007

 */



/* 

    :inject_check() 

    :           SQL     ,    ,        

    :$sql_str:       

     :      ,ture or false 

    :heiyeluren 

*/ 

function inject_check($sql_str) {  

  return eregi('select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql_str);    //       

}  

 

/* 

    :verify_id() 

    :     ID       

    :$id:    ID  

     :      ID 

    :heiyeluren 

*/ 

function verify_id($id=null) {  

  if (!$id) { exit('      !'); }    //         

  elseif (inject_check($id)) { exit('       !'); }    //       

  elseif (!is_numeric($id)) { exit('       !'); }    //       

  $id = intval($id);    //      

 

  return  $id;  

}  

 

/* 

    :str_check() 

    :            

    :$var:         

     :          

    :heiyeluren 

*/ 

function str_check( $str ) {  

  if (!get_magic_quotes_gpc()) {    //   magic_quotes_gpc      

    $str = addslashes($str);    //       

  }  

  $str = str_replace("_", "\_", $str);    //   '_'     

  $str = str_replace("%", "\%", $str);    //   '%'     

 

  return $str;   

}  

 

/* 

    :post_check() 

    :             

    :$post:        

     :$post:          

    :heiyeluren 

*/ 

function post_check($post) {  

  if (!get_magic_quotes_gpc()) {    //   magic_quotes_gpc       

    $post = addslashes($post);    //   magic_quotes_gpc                 

  }  

  $post = str_replace("_", "\_", $post);    //   '_'     

  $post = str_replace("%", "\%", $post);    //   '%'     

  $post = nl2br($post);    //       

  $post = htmlspecialchars($post);    // html      

 

  return $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;

}



?>