好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

简单的php登录过滤防止sql注入方法

最近在了解SQL注入,发现很多人登录功能都是同时使用用户输入的 username 和 password ,组合到一条sql语句里面,查询判断有无结果来判定是否可登录。例如下面:

<?php  $username = $_POST['username'];  $password = $_POST['password'];  $sql = "select * from user where username='{$username}' and password='{$password}' ";  $this->db->query($sql);

这样很容易在 username 参数加入 ‘ or 1=1 -- 注入,虽然这只是示例,但现在很多使用框架,一不注意,最终组合成的语句也是类似这样的。这里不说输入参数过滤、参数绑定,语法分析等常用办法去应对SQL注入,我们可以转变一下判断方法:

<?php  $username = $_POST['username'];  $password = $_POST['password'];  $sql = "select * from user where username='{$username}' ";  $data = $this->db->query($sql);  ......  if($data['password'] == $password){      //密码OK  }else{      //密码错误  }

这样先查到数据,再用php本身来判断是否匹配密码,是不是解决了问题了?

查看更多关于简单的php登录过滤防止sql注入方法的详细内容...

  阅读:73次