简体   繁体   English

区分大小写的密码查询而不是用户名

[英]Case sensitive password query but not username

重新短语此查询以使用户名区分大小写但密码区分大小写的最简单方法什么?

$query = mysql_query("select * from login where password='$password' AND username='$username'", $connection) or die(mysql_error());

The correct way to do this is to let the database handle the case sensitivity. 正确的方法是让数据库处理区分大小写。 In order to do that you have to change the collation of the field username or password (it depends of their current collation). 为此,您必须更改字段usernamepassword的排序规则(取决于它们的当前排序规则)。

All you have to do is to change the collation of the field username to a collation whose name ends with _ci (it stands for case insensitive ) and the collation of the field password to a collation whose name ends with _cs (from case sensitive ). 您要做的就是将字段username的排序规则更改为名称以_ci结尾的排序_ci (表示case insensitive ),将字段password的排序规则password为名称以_cs结尾的排序_cs (从case sensitive )。

The actual names of the collations depend on the character set of your table. 排序规则的实际名称取决于表的字符集。

For example, this is the definition of a table that uses latin1 as a charset: 例如,这是一个使用latin1作为字符集的表的定义:

CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(32) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
  `password` varchar(32) CHARACTER SET latin1 COLLATE latin1_general_cs DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

If you change the collation you don't need to worry about the case sensitivity of the values any more and you don't need to change the queries. 如果您更改排序规则,则无需再担心值的区分大小写,也无需更改查询。

Sample code using the table above 使用上表的示例代码

mysql> INSERT INTO `users` (`username`, `password`)
    -> VALUES ('John Doe', 'passwd1'), ('john doe', 'PASSWD1');
Query OK, 2 rows affected (0.00 sec)


mysql> SELECT * FROM `users` WHERE `username` = 'JOHN DOE';
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  1 | John Doe | passwd1  |
|  2 | john doe | PASSWD1  |
+----+----------+----------+
2 rows in set (0.00 sec)


mysql> SELECT * FROM `users` WHERE `password` = 'passwd1';
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  1 | John Doe | passwd1  |
+----+----------+----------+
1 row in set (0.00 sec)

Alternative solution, using the excellent suggestion of @spencer7593 替代解决方案,使用@ spencer7593的出色建议

You don't need to change the collation of the fields (it is recommended but if you cannot do it then this is an alternative solution). 您不需要更改字段的排序规则(建议这样做,但是如果不能这样做,那么这是一种替代解决方案)。 You can force the collation you want in the query: 您可以在查询中强制使用所需的排序规则:

Force case sensitive comparison of username : 强制区分大小写的username比较:

mysql> SELECT * FROM `users` WHERE `username` COLLATE latin1_general_cs = 'john doe';
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  2 | john doe | PASSWD1  |
+----+----------+----------+
1 row in set (0.00 sec)

Force case insensitive comparison of password : 强制不区分大小写的password比较:

mysql> SELECT * FROM `users` WHERE `password` COLLATE latin1_general_ci = 'passwd1';
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  1 | John Doe | passwd1  |
|  2 | john doe | PASSWD1  |
+----+----------+----------+
2 rows in set (0.00 sec)

Just normalise the case. 只是规范化情况。

$query = mysql_query(
    "
        SELECT *
        FROM login
        WHERE
            password='$password'
            AND LOWER(username)=LOWER('$username')
    ",
    $connection
) or die(mysql_error());

Quite Simply you can use a LIKE , for example: 非常简单,您可以使用LIKE ,例如:

$query = mysql_query("select * from login where password='$password' AND username LIKE '$username'", $connection) or die(mysql_error());

Alternatively you can do partial matches like so, but this is not what you need: 另外,您可以像这样进行部分匹配,但这不是您所需要的:

$query = mysql_query("select * from login where password='$password' AND username LIKE '%$username%'", $connection) or die(mysql_error());

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

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