最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
PHP登录中的防止sql注入方法分析
时间:2022-06-24 15:46:32 编辑:袖梨 来源:一聚教程网
比如以下一段登录的代码:
代码如下 | 复制代码 |
if($l = @mysql_connect('localhost', 'root', '123')) or die('数据库连接失败'); mysql_select_db('test'); mysql_set_charset('utf8'); $sql = 'select * from test where username = "$username" and password = "$password"'; $res = mysql_query($sql); if(mysql_num_rows($res)){ header('Location:./home.php'); }else{ die('输入有误'); } |
注意上面的sql语句,存在很大的安全隐患,如果使用以下万能密码和万能用户名,那么可以轻松进入页面:
代码如下 | 复制代码 |
1. $sql = 'select * from test where username = "***" and password = "***" or 1 = "1"'; |
很明显,针对这条sql语句的万能密码是: ***" or 1 = "1
代码如下 | 复制代码 |
2. $sql = 'select * from test where username ="***" union select * from users/* and password = "***"'; |
正斜线* 表示后面的不执行,mysql支持union联合查询, 所以直接查询出所有数据; 所以针对这条sql语句的万能用户名是:***" union select * from users/*
但是,此注入只针对代码中的sql语句,如果
代码如下 | 复制代码 |
$sql = "select * from test where username = $username and password = $password"; |
上面的注入至少已经不管用了,不过方法是一样的;
在使用PDO之后,sql注入完全可以被避免,而且在这个快速开发的时代,框架横行,已然不用过多考虑sql注入问题了。
下面整理了两个防止sql注册函数
代码如下 | 复制代码 |
/* 过滤所有GET过来变量 */ |
还有一些博客会这样写
代码如下 | 复制代码 |
function post_check($post) |
相关文章
- 崩坏星穹铁道知更鸟养成建议 崩坏星穹铁道知更鸟怎么培养 05-02
- 原神4.6深境螺旋12层满星攻略 原神4.6深渊12层怎么满星通关 05-02
- 崩坏星穹铁道知更鸟养成建议 05-02
- 崩坏星穹铁道2.1虚构叙事第二期追击队怎么打 05-02
- 崩坏星穹铁道2.2上半UP光锥适用角色推荐 05-02
- 崩坏星穹铁道2.2上半光锥卡池一览 05-02