看书的时候,看到说:“直接查询数据库验证用户名和密码,不会区分用户名和密码的大小写”。自己验证了一下,结果确实不会区分(代码如下)
。说是建议用函数strcmp()进行比较。
然而,我去baidu上查找相关资料发现说:“strcmp()函数有安全漏洞——构造数组绕过"。所有我想问问各位大侠,现在验证用户名和密码常见的方法是什么?
html页面代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆页面</title>
</head>
<body>
<center>
<h2>登陆页面</h2>
</center>
<form action="login_pro.php" method="post">
<table align='center'>
<tr>
<td>用户名:</td>
<td><input type="text" name="user"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="pwd"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
php页面验证代码
<?php
header('content-type:text/html;charset=utf-8');
foreach($_POST as $key => $value)
{
$$key = $value;
}
//var_dump($pwd);die;
$pdo = new PDO('mysql:host=127.0.0.1;dbname=test',"root","root");
$pdo -> exec("set names utf8");
$res = $pdo -> query("select * from ci_user where user ='".$user."' and pwd ='".$pwd."'")->fetch(PDO::FETCH_ASSOC);
// var_dump($res);die;
if($res)
{
echo "<script>alert('用户验证通过。');history.back();</script>";
}
else
{
echo "<script>alert('密码验证错误。');history.back();</script>";
}