博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php sql防注入
阅读量:4663 次
发布时间:2019-06-09

本文共 1410 字,大约阅读时间需要 4 分钟。

没有太多的过滤,主要是针对php和mysql的组合。

一般性的防注入,只要使用php的 addslashes 函数就可以了。

以下是一段copy来的代码:

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. }  

做系统的话,可以用下面的代码,也是copy来的。

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. }  

转载于:https://www.cnblogs.com/zonglonglong/p/5031619.html

你可能感兴趣的文章
[SinGuLaRiTy] 2017 百度之星程序设计大赛 复赛
查看>>
hard-negative mining 及伪代码实现
查看>>
JS框架_(Laydate.js)简单实现日期日历
查看>>
19. Remove Nth Node From End of List
查看>>
Struts2(三):新建Struts2工程
查看>>
数据库调优过程(一):SqlServer批量复制(bcp)[C#SqlBulkCopy]性能极低问题
查看>>
AS中jar包和aar包区别及导入导出
查看>>
Android系统备忘1
查看>>
tomcat配置
查看>>
C语言学习之路
查看>>
值-结果参数
查看>>
[Excel] C# ExcelHelper操作类 (转载)
查看>>
使用jsoup进行网页内容抓取
查看>>
深入理解JVM内幕:从基本结构到Java 7新特性
查看>>
[NodeJs]入门经典
查看>>
准确判断listview上下滚动
查看>>
codeforces666A
查看>>
比较真实的下雪效果
查看>>
MongoDB 3.2 从安装到使用。
查看>>
CFround#380 div2
查看>>