Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1549453
  • 博文数量: 157
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4116
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-14 18:26
文章分类

全部博文(157)

文章存档

2014年(67)

2013年(90)

分类: 数据库开发技术

2013-07-25 10:37:41

(1)mysql_real_escape_string -- 转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集 

 
使用方法如下:
 
?
1
2
3
$sql = "select count(*) as ctr from users where username
='".mysql_real_escape_string($username)."' and
password='". mysql_real_escape_string($pw)."' limit 1";

 

 
 
 
使用 
mysql_real_escape_string()
 作为用户输入的包装器,就可以避免用户输入中的任何恶意 SQL 注入。
 
(2) 打开magic_quotes_gpc来防止SQL注入
 
php.ini中有一个设置:magic_quotes_gpc = Off
  这个默认是关闭的,如果它打开后将自动把用户提交对sql的查询进行转换,
  比如把 ' 转为 \'等,对于防止sql注射有重大作用。
 
     如果magic_quotes_gpc=Off,则使用addslashes()函数
 
(3)自定义函数
 
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function inject_check($sql_str) {
    return eregi('select|insert|and|or|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile', $sql_str);
}
 
function verify_id($id=null) {
    if(!$id) {
        exit('没有提交参数!');
    } elseif(inject_check($id)) {
        exit('提交的参数非法!');
    } elseif(!is_numeric($id)) {
        exit('提交的参数非法!');
    }
    $id = intval($id);
      
    return $id;
}
 
 
function str_check( $str ) {
    if(!get_magic_quotes_gpc()) {
        $str = addslashes($str); // 进行过滤
    }
    $str = str_replace("_", "\_", $str);
    $str = str_replace("%", "\%", $str);
      
   return $str;
}
 
 
function post_check($post) {
    if(!get_magic_quotes_gpc()) {
        $post = addslashes($post);
    }
    $post = str_replace("_", "\_", $post);
    $post = str_replace("%", "\%", $post);
    $post = nl2br($post);
    $post = htmlspecialchars($post);
      
    return $post;
}

 

阅读(1169) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~