作为一名设计师,我不熟悉PHP + MySQL。对于我们的一个项目,我们创建了一个“重置密码”功能,该功能通过电子邮件将新密码发送给用户。由于我们用户的反馈,我们也想在此电子邮件中添加用户名。
这是我们目前使用的代码:
class password {
private $newpassword;
private $passwordlength = 10;
function __construct() {
$this->newpassword = $this->getRandomPassword($this->passwordlength);
}
// This function resets the password
function resetPassword($email) {
mysql_query("UPDATE users
SET password = '".md5($this->newpassword)."'
WHERE email = '".mysql_real_escape_string($email)."'")
or die(mysql_error());
$this->sendNewPassword($email);
}
// Check if email is exists
function checkEmailAdress($email) {
$correctemail = false;
$queryEmail = mysql_query("SELECT username FROM users
WHERE email = '".mysql_real_escape_string($email)."'")
or die(mysql_error());
if(mysql_num_rows($queryEmail) == 1) {
$correctemail = true;
}
return $correctemail;
}
// Send new password with email
function sendNewPassword($email) {
$onderwerp = 'New password for ... ';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: info@domain.com' . "\r\n" .
'Reply-To: info@domain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$bericht = '<div>
Your new password is: '.$this->newpassword.'
</div>
<div>
Your username is: USERNAME HERE.
</div>
';
mail($email, $onderwerp, $bericht, $headers);
}
// Generate random password
function getRandomPassword($length) {
$characters = "0123456789abcdefghijklmnopqrstuvwxyz";
$string = "";
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}
}
用户名存储在表'username'中。
如何检索用户名并将其添加到'$ bericht'(消息)
中答案 0 :(得分:1)
在您的功能中添加此
function resetPassword($email) {
mysql_query("UPDATE users
SET password = '".md5($this->newpassword)."'
WHERE email = '".mysql_real_escape_string($email)."'")
or die(mysql_error());
$result = mysql_query("SELECT username as username FROM users
WHERE email = '".mysql_real_escape_string($email)."'")
or die(mysql_error());
$my_res = mysql_result($result);
$username = $my_res[0]['username'];
$this->sendNewPassword($email,$username);
}
并在您的邮件中添加
$bericht = '<div>
Dear User: '.$username.'
</div>
<div>
Your new password is: '.$this->newpassword.'
</div>
<div>
Your username is: USERNAME HERE.
</div>
';