gpt4 book ai didi

php - 从数据库中获取带有当前电子邮件地址的用户名

转载 作者:行者123 更新时间:2023-11-29 13:50:17 26 4
gpt4 key购买 nike

作为一名设计师,我对 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;
}

}

用户名存储在“用户名”表中。

如何检索用户名并将其添加到“$bericht”(消息)中

最佳答案

在你的函数中添加这个

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>

';

关于php - 从数据库中获取带有当前电子邮件地址的用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16788448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com