JOE个人网站

JOE个人网站,不仅仅是一个网站,更像是一个展现自我的平台,致力于让朋友们都可以
有所感触,有所收获。

防SQL注入

2017-12-18 18:26:58
<?php
    header("content-type:text/html;charset=utf-8");
    // SQL防止注入
    // 在SQL中/*表示后面的代码都不执行了
    // 攻击
    // -------------------------------------------------
    // 问题SQL:select * from user where username='$username' and password='$password';
    // 万能密码
    // bb' or 1='1
    // 万能用户名
    // xx' union select * from users/*
    // 问题SQL:select * from user where username=$username and password=$password;
    // 万能密码
    // 帐号和密码随便输入一个数字
    // 33 union select * from user;
    // 万能用户名
    // 33 union select * from user/*
    // 防范
    // --------------------------------------------------
    // 1. 服务器
    // 		magic_quotes_gpc设置为On:服务器就会对所有的'加入转义
    // 			在SQL语句中可以用char(96)代替'从而躲避这种设置
    // 		display_errors设置为Off
    // 2. 代码本身:往往有时候改不了服务器配置,只能通过代码本身来防范
    // a. 如果非要用上面那种联合查询,用引号引起来后,用addslashes函数对用户输入的数据进行转义
    // b. 密码比对:首先通过用户输入的用户名去查询数据库,如果查到这个用户所对应的密码,则和用户提交的密码比对,相同则说明该用户合法,反之则说明该用户非法
    // c. PDO预处理

    // 模糊查询的SQL攻击
    // --------------------------------------------------
    // SQL语句:select * from user where username like '%$username%';
    // 比如输入%或__都可以显示全部数据
    // 如何防止
    // 对关键字进行过滤
    // $keyWord = addslashes($keyWord);
    // 对关键字进行特殊字符替换
    // $keyWord = str_replace("%","\%",$keyWord);
    // $keyWord = str_replace("_","\_",$keyWord);

    // insert into user(`username`,`pwd`,`role`) values('$username','$pwd','$role');
    // 也会有万能用户名和万能密码的那种攻击,导致别人的注册帐号成了管理员权限
    // insert into user(`username`,`pwd`,`role`) values('joe','joe',3)/*',1);
?>