简体   繁体   English

尽管ci整理,SQL假设区分大小写?

[英]SQL assume case sensitive despite ci collation?

In my users table, I have a row with username = Q . 在我的users表中,我有一行username = Q When I try to log-in as "q" instead, it states the user does not exist, however with "Q" it works as intended. 当我尝试以“q”登录时,它表示用户不存在,但是“Q”按预期工作。 Here's the login check code. 这是登录检查代码。

$username = $_POST["name"];
$password = $_POST["pass"];
$pahash = crypt($password, $username);
$logcheck = $db->prepare("
    SELECT *
        FROM users
        WHERE username = :username
        AND password = :pahash
");
$logcheck->execute(array(
    ':username' => $username,
    ':pahash' => $pahash));

The collation for pretty much every single column in my database is "latin1_swedish_ci", which is case insensitive. 我数据库中几乎每一列的排序规则都是“latin1_swedish_ci”,它不区分大小写。 Why is it, then, that it seems to be working as case sensitive? 那么,为什么它似乎是区分大小写的呢?

If it matters, the type for the user column is char(20). 如果重要,则用户列的类型为char(20)。

you could do: 你可以这样做:

$username = $_POST["name"];
$password = $_POST["pass"];
$username = strtolower($username);
$pahash = crypt($password, $username);
$logcheck = $db->prepare("
    SELECT *
        FROM users
        WHERE LOWER(username) = :username
        AND password = :pahash
");
$logcheck->execute(array(
    ':username' => $username,
    ':pahash' => $pahash));

Use "LIKE" instead of "=" for username: 使用“LIKE”代替“=”用户名:

SELECT *
    FROM users
    WHERE username LIKE :username
    AND password = :pahash

The problem is that crypt is not case insensitive. 问题是crypt不区分大小写。 So the value of $pahash is different for the same value of $password when the value of $username changes from Q to q . 因此,当$username的值从Q变为q时, $pahash的值对于$password的相同值是不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM