php sql注入防止

2114 ワード

discuzフォーラムのsql注入防止関数:
$magic_quotes_gpc = get_magic_quotes_gpc(); 
@extract(daddslashes($_COOKIE)); 
@extract(daddslashes($_POST)); 
@extract(daddslashes($_GET)); 
if(!$magic_quotes_gpc) { 
$_FILES = daddslashes($_FILES); 
} 


function daddslashes($string, $force = 0) { 
if(!$GLOBALS['magic_quotes_gpc'] || $force) { 
if(is_array($string)) { 
foreach($string as $key => $val) { 
$string[$key] = daddslashes($val, $force); 
} 
} else { 
$string = addslashes($string); 
} 
} 
return $string; 
}

以下のコードを強化してサーバーの安全を保護することができます.PHPはSQL注入安全関数を防止することが重要です. 
/* 
    :inject_check() 
    :           SQL     ,    ,        
    :$sql_str:       
     :      ,ture or false 
*/ 
function inject_check($sql_str) { 
return eregi('select|insert|and|or|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql_str); //      
} 

/* 
    :verify_id() 
    :     ID       
    :$id:    ID  
     :      ID 
*/ 
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:         
     :          
*/ 
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:          
*/ 
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; 
}