PDO通过预处理语句防止sql注入

    xiaoxiao2025-06-07  14

    简单登录页面防注入

    数据库:test;

    建表
    CREATE TABLE USER( id INT UNSIGNED AUTO_INCREMENT KEY, username VARCHAR(20) NOT NULL, password CHAR(32) NOT NULL, email CHAR(32) NOT NULL );
    向表中插入一条数据:
    INSERT INTO user(username,password,email) VALUES('user','root','123@qq.com');
    登入界面:
    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>login</title> </head> <body> <form action="check.php" method="post" accept-charset="utf-8"> 用户名:<input type="text" name="username"><br> 密 码:<input type="password" name="psw"><br> <input type="submit"value="提交"> </form> </body> </html>
    1.首先先正常编写check.php页面,不做任何处理。
    header('content-type:text/html;charset=utf-8'); $username=$_POST['username']; $psw=$_POST['psw']; try { $pdo=new PDO('mysql:host=localhost;dbname=test','root','root'); $sql="select * from user where username='{$username}' and password='{$psw}'"; $stmt=$pdo->query($sql);//返回结果集中的行数echo $stmt->rowCount(); } catch (Exception $e) { echo $e->getMessage(); }

    执行操作 输入正确的用户名:user 密码:root 结果:1(因为查询到一条记录 通过echo rowCount()输出) 输入错误的用户名或密码 结果:0 输入注入语句 用户名:'or 1=1 # 密码:随便写 结果:1

    2.更换为预处理语句(占位符)
    第一种
    header('content-type:text/html;charset=utf-8'); $username=$_POST['username']; $psw=$_POST['psw']; try { $pdo=new PDO('mysql:host=localhost;dbname=test','root','root'); $sql="select * from user where username=:username and password=:psw"; $stmt=$pdo->prepare($sql); $stmt->execute(array(":username"=>$username,":psw"=>$psw)); echo $stmt->rowCount(); } catch (Exception $e) { echo $e->getMessage(); }
    第二种
    header('content-type:text/html;charset=utf-8'); $username=$_POST['username']; $psw=$_POST['psw']; try { $pdo=new PDO('mysql:host=localhost;dbname=test','root','root'); $sql="select * from user where username=? and password=?"; $stmt=$pdo->prepare($sql); $stmt->execute(array($username,$psw)); echo $stmt->rowCount(); } catch (Exception $e) { echo $e->getMessage(); }

    执行操作 输入注入语句 用户名:'or 1=1 #  密码:随便写      结果:0   说明已经无法注入。

    最新回复(0)