PHP 中的 MySQL 密码比较

发布于 2024-11-27 12:09:10 字数 337 浏览 1 评论 0原文

下午好。基本上我有一个带有登录表单的页面,要求用户输入用户名和密码。然后我将这些用户名和密码的哈希版本与 MySQL 中的密码数据库进行匹配。

现在,由于我不知道黑客/漏洞利用者有什么能力,我只想永远不要从数据库中检索正确密码的散列版本并在服务器上进行比较。我已经搜索了谷歌和 MySQL 教程网站,但我似乎无法得到明确的答案(我可能在这里淹没在浅水里)。基本上这就是我想要做的:

  • 用户输入用户名+密码。
  • 我通过存储过程将此信息转发到 MySQL。
  • 如果找到匹配项,存储过程将返回 TRUE/FALSE(1 或 0 并不重要)。

非常感谢您抽出时间。

Afternoon SO. Basically I have a page with a login form where the user is asked to input their username and password. Then i match these username and a hashed version of the password with the Passwords database in MySQL.

Now, seeing as I have no idea what hackers/expoilters are capable of i would just like to never retrieve the hashed version of the right password from the database and compare it on the server. I have scoured google and the MySQL tutorial websites but I cant seem to get a clear answer (i might be drowning in shallow water here). Basically this is what i want to do:

  • User inputs username + password.
  • I forward this information to MySQL through a stored procedure.
  • Stored procedure returns TRUE/FALSE (1 or 0 doesnt matter) if a match is found.

Thankyou very much for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裸钻 2024-12-04 12:09:10

我不太确定你在做什么,但如果我正确理解了这个问题,你想做这样的事情:

SELECT password FROM users WHERE username = '$username';

然后将密码与 PHP 进行比较,你担心这可能会成为一个安全问题。

我认为这从安全角度来看没有任何区别。检查密码是否正确的常用方法是:

SELECT COUNT(*) FROM users WHERE username = '$username' AND password = '$hashedPassword';

如果 count == 1,则用户给出了正确的密码。您可以看到,如果密码不匹配,MySQL 在任何时候都不会给出实际密码。即使确实如此,黑客也几乎不可能访问该信息。如果他们可以,他们已经劫持了您的服务器,这是您遇到的最小问题。

I'm not quite sure what you're after, but if I understood the question correctly, you want to do something like this:

SELECT password FROM users WHERE username = '$username';

and then compare the passwords with PHP, and you're concerned that this might become a security issue.

I don't think it makes any difference in the security standpoint. The usual way of checking if the password is correct is:

SELECT COUNT(*) FROM users WHERE username = '$username' AND password = '$hashedPassword';

If count == 1, the user gave the correct password. You can see that MySQL doesn't give the actual password at any point if the passwords don't match. Even if it did, it's nearly impossible for a hacker to get access to that information. If they can, they have already hijacked your server and this is the least of your problems.

倥絔 2024-12-04 12:09:10

将凭据发送到数据库时只需使用 mysql_real_escape_string 即可。这将防止 SQL 注入。

<?php
// Connect
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
    OR die(mysql_error());

// Query
$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
            mysql_real_escape_string($user),
            mysql_real_escape_string($password));
?>

http://www.php.net/manual/en /function.mysql-real-escape-string.php

Just use mysql_real_escape_string when sending the credentials to your database. This will prevent SQL injection.

<?php
// Connect
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
    OR die(mysql_error());

// Query
$query = sprintf("SELECT * FROM users WHERE user='%s' AND password='%s'",
            mysql_real_escape_string($user),
            mysql_real_escape_string($password));
?>

http://www.php.net/manual/en/function.mysql-real-escape-string.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文