所以我正在构建一个 localhost 页面,它包含用户,所以我创建了一个忘记密码页面来在他们忘记密码时重置密码,并将随机密码更新到数据库中。
这是 ForgotPassword 的代码,我通过验证电子邮件而不是 ID 来使用它。
<h2 style="text-align:center;"> Forgot Password </h2>
<table align="center" bgcolor="#FFFFFF" width="500" height ="180">
<form action="doForgotPassword.php" method="post">
<tr><td align="right" height="50" width="200">First Name</td>
<td><input type="text" name="first_name" /></td></tr>
<tr><td align="right" height="50">Email</td>
<td><input type="text" name="email" /></td></tr>
<tr><td colspan="2" align="right"><input type="submit" value="Submit"/></td></tr>
</table> </form> </td></table>
这是 DoForgotPassword 的代码,我设法在数据库中更改了它,但无法通过电子邮件发送给用户。我在这条线上遇到了麻烦
<?php
public function sendMail($email =$_POST['email'] , $id)
这是它之后的其余代码。
{
$to = $email /* separated by comma for another email (useful if you want to keep records(sending to yourself))*/;
$subject = 'INSERT_SUBJECT_HERE';
$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";
$headers = "From: noreply@somewhere.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;
$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;
$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'
<font size="3" color="#000000" style="text-decoration:none;font-family:Lato light">
<div class="info" Style="align:left;">
<p>information here<!--(im sure you know how to write html ;))--></p>
<br>
'. /* <p>Charge: '.$charge.' </p> */'
<br>
<p>Reference Number: '.$id.'</p>
</div>
</br>
<p>-----------------------------------------------------------------------------------------------------------------</p>
</br>
<p>( This is an automated message, please do not reply to this message, if you have any queries please contact someone@someemail.com )</p>
</font>
</div>
</body>
'."\n\n".
$bound_last;
$sent = mail($to, $subject, $message, $headers); // finally sending the email
}
$email =$_POST['email'];
function createRandomPassword() {
$chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789";
$i = 0;
$pass = '' ;
while ($i <= 8) {
$num = mt_rand(0,61);
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$pw = createRandomPassword();
$query = "UPDATE users SET password= SHA1('$pw') WHERE email = '$email' ";
$result = mysqli_query($link, $query);
if ($result){
$query3 = "SELECT * FROM users where email = '$email'";
$sql = mysqli_query($link, $query3) or die(mysqli_error());
$rownum = mysqli_num_rows($sql);
if(!$rownum ) {
echo "We can not find your email in our records";
}
}
这是生成的代码
if($result){
if(isset($_POST['id'])){
$id = $_POST['id'];
sendMail($email, $id);
}
}
?>