查询SQL获取用户名并返回密码
我有一个用户名和密码的数据库。我需要创建一个“忘记密码”功能,并让它在表中搜索用户名并返回该用户的密码。然后我希望它发送一封电子邮件,说明姓名和密码。
这是我用于查询特定用户的数据库的工作代码:
<?php
session_start();
include "config.php";
if($_POST['nameQuery']) {
$query = "SELECT * FROM myDatabase WHERE name = '" .$_POST['nameQuery']. "'";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
//User exists
echo '1';
} else {
mysql_query($query);
//User does not exist
echo '0';
}
}
?>
I have a database of usernames and passwords. I need to create a "Forgot password" function and have it search the table for a username and return that user's password. Then I would like it to send an email saying the name and password.
Here is my working code for querying the database for a specific user:
<?php
session_start();
include "config.php";
if($_POST['nameQuery']) {
$query = "SELECT * FROM myDatabase WHERE name = '" .$_POST['nameQuery']. "'";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
//User exists
echo '1';
} else {
mysql_query($query);
//User does not exist
echo '0';
}
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
首先要做的事情是:您可能想确保您不会通过登录获得
SQL 注入
,因为您实际上是将用户输入注入到您的查询中......这是一个大禁忌。交换这个:
...为此:
接下来是您所要求的:一种获取用户用户名和密码的方法。虽然我不建议您实际上以明文形式存储密码以供每个人查看,但这是您必须自行做出的决定。
此代码片段将执行此操作:
编辑:添加密码恢复功能
对于您在下面请求的密码恢复功能,您可以尝试如下操作:
recover_password.php:
注意我还没有时间实际测试代码,但我认为通过一些细微的调整就可以正常工作。哦,在我忘记之前,您需要将下表添加到数据库中:
表
myRequests
的表结构祝你好运!
First thing's first: you might want to make sure that you won't get
SQL-injected
via your login, as you're literally injecting the user input into your query... big no-no.Swap this:
...for this:
Next up is what you asked for: a way to get both the users username and password. While I don't recommend that you actually store the password in plaintext for everyone to view, it's a decision you have to make on your own.
This snippet will do the deed:
Edit: Adding password recovery-functionality
For the password recovery-functionality you requested below, you can try something like this:
recover_password.php:
Note that I haven't had time to actually test the code, but I think it'll work just fine with some minor adjustments. Oh, before I forget, you'll need to add the following table to the DB:
Table structure for table
myRequests
Good luck!
虽然与您原来的问题无关,但我想指出,以纯文本形式存储密码是一个坏主意。您应该将密码的哈希版本存储在数据库中。然后,您可以对用户输入进行哈希处理,并将其与登录数据库中的内容进行比较。
相反,您忘记的密码应该创建一个新的(临时)密码,并将该哈希值存储在数据库中,同时将纯文本密码发送到存档的电子邮件帐户。
While tangential to your original question, I would like to point out that storing passwords in plain text is a bad idea. You should store hashed versions of the password in the database. You can then hash user input and compare it to what is in the database for logging in.
Instead, your forgot password should create a new(temporary) password, and store the hash of that in the database, while sending the plain text password to the email account on file.
只需阅读结果:
更一般地说:您的代码中存在 SQL 注入漏洞,请查看该主题,否则攻击者将能够读取您所有用户的密码。
另外,不建议将密码以明文形式存储在数据库中。请使用 sha1 或 sha256 等哈希算法来存储密码。
Just read the result:
On a more general note: You have a SQL injection vulnerability in your code, please look into that topic, or attackers will be able to read all your user's passwords.
Also, it is not advised to store the password in clear text in you database. Please use a hashing algorithm like sha1 oder sha256 to store passwords.
我建议您将表设计更改为
登录时根据哈希密码检查用户,类似这样
登录时然后使用 sql
从 tbl 中选择 col1,col2,..,其中 user=?和密码=?然后用 $_POST['username'], $_POST['password'] 填充参数,
因此使用 Prepared Statement 或 PDO
当用户忘记密码时使用相同的逻辑
I will recommend you to change your table design to
When login check the user against the hashed password, something like this
When loggin in then use sql like
select col1,col2,.. from tbl where user=? and password=? and then fill the parameter with $_POST['username'], $_POST['password']
so use Prepared Statement or PDO
use the same logic when user forgot his password
您必须从 mysql_query 结果(存储在
$result
变量中)检索用户名和密码,如下所示:然后使用 php 的 mail() 函数发送电子邮件。
You have to retrieve the username and password from the mysql_query result (stored in the
$result
variable) as such:Then use php's mail() function to send the e-mail.
请勿将密码存储在数据库中。绝不能存储明文密码。您应该存储密码的哈希值,以帮助防止它们在其他网站上使用。有关详细信息,请参阅在数据库中存储密码的最佳方式。
DO NOT store passwords in your database. Cleartext passwords should never be stored. You should be storing a hash of the passwords to help prevent them being used on other sites. See Best way to store password in database for more information.
您的代码不安全!您的
$_POST['nameQuery']
是通往 SQL 注入最低的安全性是转义并清理所有输入
黄金法则:永远不要相信传入的数据。
Your code is NOT secured ! Your
$_POST['nameQuery']
is a gorgeous opened door to SQL InjectionThe minimum security is to escape and sanitize all your inputs
The golden rule: never trust incoming data.
社区维基:
不要。因为这意味着您将保存可检索的密码。最好向他们的电子邮件发送一个密码更改链接,以便访问一次性密码重置页面。这样,在有权访问该用户电子邮件的人完成重置周期之前,密码不会更改。
通过这种方式,您可以适当地散列密码并仅根据散列检查传入的密码。
此外,我建议查看 php 的 PDO,因为您当前正在创建容易受到 sql 注入影响的 sql 查询。
Community Wiki:
Don't. Because that means you'll be saving retrievable passwords. Better to send a password-changing link to their email that gives access to a one-time password reset page. In this way, the password isn't changed until a reset cycle is completed by someone with access to that user's email.
In that way you can appropriately hash passwords and check incoming passwords against a hash only.
In addition, I recommend looking into php's PDO, because you're currently creating sql queries that are succeptible to sql-injection.
我有一些建议给你
祝你好运,编码愉快
I have a few suggestions for you
good luck and happy coding