Php 忘记密码页面,创建生成的密码以通过电子邮件发送给用户。
我正在尝试创建一个忘记密码页面。我听说通过电子邮件将原始密码发送给用户不是一个好主意,因此我尝试创建一个随机确认密码,他们可以使用该密码登录自己的帐户,然后将密码更改为他们想要的任何密码。到目前为止,问题是说用户的电子邮件不在数据库中,而实际上它在数据库中。另外,我应该更新数据库来存储随机密码,还是我的工作方式?我的数据库中有表username、fristname、email和password。我要求用户以html格式输入他们的电子邮件地址,然后将其发送到此php表单。这是我第一次尝试这样做,所以它可能有很多错误,但我找到了一个教程来帮助一些人,所以它不应该。谢谢你的帮助Php 忘记密码页面,创建生成的密码以通过电子邮件发送给用户。,php,sql,phpmyadmin,Php,Sql,Phpmyadmin,我正在尝试创建一个忘记密码页面。我听说通过电子邮件将原始密码发送给用户不是一个好主意,因此我尝试创建一个随机确认密码,他们可以使用该密码登录自己的帐户,然后将密码更改为他们想要的任何密码。到目前为止,问题是说用户的电子邮件不在数据库中,而实际上它在数据库中。另外,我应该更新数据库来存储随机密码,还是我的工作方式?我的数据库中有表username、fristname、email和password。我要求用户以html格式输入他们的电子邮件地址,然后将其发送到此php表单。这是我第一次尝试这样做,所
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sending Password</title>
</head>
<body>
<?php
$db_server = "server";
$db_username = "name";
$db_password = "pass";
$con = mysql_connect($db_server, $db_username, $db_password);if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$database = "Test_Members";
$er = mysql_select_db($db_username);
if (!$er)
{
print ("Error - Could not select the database");
exit;
}
//include "session.php";
function createRandomPassword() {
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
srand((double)microtime()*1000000);
$i = 0;
$pass = '' ;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$password = createRandomPassword();
$password =$_P0ST['password'];
$email = $_P0ST['email'];
$tbl_name=Account_Holders;
$sql="SELECT password FROM $tbl_name WHERE email='$email'";
$result=mysql_query($sql);
// if found this e-mail address, row must be 1 row
// keep value in variable name "$count"
$count=mysql_num_rows($result);
// compare if $count =1 row
if($count==1){
$rows=mysql_fetch_array($result);
// keep password in $your_password
$your_password=$rows['password']; //will this replace the users password with the random one? That is what I am attempting to do here.
// send e-mail to ...
$to=$email;
// Your subject
$subject="Your Password";
// From
$header="from: Feed The Students";
// Your message
$messages= "Your password for login to our website \r\n";
$messages.="Your password is $your_password \r\n";
$messages.="Please change this password for security reasons. Thank you. \r\n";
// send email
$sentmail = mail($to,$subject,$messages,$header);
}
// else if $count not equal 1
else {
echo "Sorry we did not find your email in our database.";
}
// if your email succesfully sent
if($sentmail){
echo "Your password has been sent to your email address.";
}
else {
echo "We can not send your password at this time.";
}
?>
</body>
</html>
发送密码
我建议:
1-用户单击忘记通过,并写入用户名和/或电子邮件。检查数据库中是否存在用户名和/或电子邮件,如果为真,则生成随机代码并将其存储在数据库中。
2-向用户发送一封带有以下链接的电子邮件:ww.yourwebsite.com/forget?recovery=code生成
3-用户单击链接,然后检查数据库中是否存在该代码,如果为真,则显示一张表单以填写新密码。用户填写,然后保存数据库。
4-完成
问题:如果用户没有密码,为什么要求用户填写密码字段
$password =$_P0ST['password'];
还有,为什么不使用:
$your_password = createRandomPassword();
相反:
$your_password=$rows['password'];
为什么会有这两条线:
$password = createRandomPassword();
$password =$_P0ST['password'];
第一种方法生成一个新密码,但您设置了发布在同一变量上的值。我建议:
1-用户单击忘记通过,并写入用户名和/或电子邮件。检查数据库中是否存在用户名和/或电子邮件,如果为真,则生成随机代码并将其存储在数据库中。
2-向用户发送一封带有以下链接的电子邮件:ww.yourwebsite.com/forget?recovery=code生成
3-用户单击链接,然后检查数据库中是否存在该代码,如果为真,则显示一张表单以填写新密码。用户填写,然后保存数据库。
4-完成
问题:如果用户没有密码,为什么要求用户填写密码字段
$password =$_P0ST['password'];
还有,为什么不使用:
$your_password = createRandomPassword();
相反:
$your_password=$rows['password'];
为什么会有这两条线:
$password = createRandomPassword();
$password =$_P0ST['password'];
第一个变量生成一个新密码,但您设置了发布在同一变量上的值。需要注意的几个问题:
注意:在数据库中存储未加密的密码通常不是一个好主意。通常要做的是首先对密码进行加密,然后以这种方式存储在数据库中。在验证用户的登录凭据时,请使用相同的加密算法加密用户发送的密码,并与数据库中的密码进行比较。需要注意的几个问题:
example.com/reset/?id=THERE\u id&token=RANDOM\u token
然后在该页面上检查他们是否匹配,如果匹配,让他们输入新密码
我听说通过电子邮件向用户发送原始密码不是一个好主意
老实说,您甚至不应该在数据库中存储原始密码,您希望在这些类型的页面上查找诸如和之类的内容-应该有其他链接供您使用教程/示例了解所有内容
最后,请务必验证您的用户输入(),因为您使用的是mysql\u query
,所以您也总是希望用它来转义输入-querys的首选方法是使用这些方法,它们非常有助于阻止诸如