一般性的防注入,只要使用php的 addslashes 函数就可以了。
PHP代码
  1. $_POST = sql_injection($_POST);  
  2. $_GET = sql_injection($_GET);  
  3.   
  4. function sql_injection($content)  
  5. {  
  6. if (!get_magic_quotes_gpc()) {  
  7. if (is_array($content)) {  
  8. foreach ($content as $key=>$value) {  
  9. $content[$key] = addslashes($value);  
  10. }  
  11. else {  
  12. addslashes($content);  
  13. }  
  14. }  
  15. return $content;  
  16. } 
做系统的话,可以用下面的代码
PHP代码
  1.      
  2. function inject_check($sql_str) {      
  3.   return eregi('select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile'$sql_str);    // 进行过滤      
  4. }      
  5.      
  6.      
  7. function verify_id($id=null) {      
  8.   if (!$id) { exit('没有提交参数!'); }    // 是否为空判断      
  9.   elseif (inject_check($id)) { exit('提交的参数非法!'); }    // 注射判断      
  10.   elseif (!is_numeric($id)) { exit('提交的参数非法!'); }    // 数字判断      
  11.   $id = intval($id);    // 整型化      
  12.      
  13.   return  $id;      
  14. }      
  15.      
  16.      
  17. function str_check( $str ) {      
  18.   if (!get_magic_quotes_gpc()) {    // 判断magic_quotes_gpc是否打开      
  19.     $str = addslashes($str);    // 进行过滤      
  20.   }      
  21.   $str = str_replace("_""\_"$str);    // 把 '_'过滤掉      
  22.   $str = str_replace("%""\%"$str);    // 把 '%'过滤掉      
  23.      
  24.   return $str;       
  25. }      
  26.      
  27.      
  28. function post_check($post) {      
  29.   if (!get_magic_quotes_gpc()) {    // 判断magic_quotes_gpc是否为打开      
  30.     $post = addslashes($post);    // 进行magic_quotes_gpc没有打开的情况对提交数据的过滤      
  31.   }      
  32.   $post = str_replace("_""\_"$post);    // 把 '_'过滤掉      
  33.   $post = str_replace("%""\%"$post);    // 把 '%'过滤掉      
  34.   $post = nl2br($post);    // 回车转换      
  35.   $post = htmlspecialchars($post);    // html标记转换      
  36.      
  37.   return $post;      
  38. }