【发布时间】:2012-01-07 04:37:18
【问题描述】:
我正在尝试创建一个忘记密码的页面。我听说通过电子邮件将原始密码发送给用户不是一个好主意,因此我正在尝试创建一个随机确认密码,他们可以使用该密码登录他们的帐户,然后将密码更改为他们想要的任何密码。到目前为止,问题是它说用户的电子邮件不在数据库中,而实际上它在数据库中。另外,我应该更新数据库以存储随机密码还是我让它工作的方式?我的数据库有表用户名、前名、电子邮件和密码。我以 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>
【问题讨论】:
-
由于htmlentities或其他原因,电子邮件可能显示为不可见,请检查@之类的内容是否已转换为它们的html实体以防万一?
标签: php sql phpmyadmin