如何在PHP中发送带有重置密码链接的电子邮件

koaltpgm  于 2023-03-07  发布在  PHP
关注(0)|答案(3)|浏览(128)

我喜欢通过my website探索PHP的世界,通过创建小项目个人谁给予我的经验,为目前和未来的工作谁知道...
我会很快添加电子邮件的信息时,用户在我的网站上创建帐户...
经过几个小时的研究和阅读,我找不到或不明白如何发送带有链接的电子邮件来重置用户的密码...
下面是我的哈希方法:

$options = ['cost' => 11, 'salt' => random_bytes(22)];
$passwordCrypter = password_hash($password, PASSWORD_BCRYPT, $options);

我认为我们需要使用这种方法,因为解密密码是不建议的,我认为...
但为什么...我想知道...
谢谢
有一个很好的家伙

lb3vh1jj

lb3vh1jj1#

在此处使用PHPMailer我的代码示例其中$dir是当前目录

function mailSet($to,$full_name,$subject,$body){
    require($dir.'vendor/mailer/PHPMailerAutoload.php');
    require($dir.'vendor/mailer/class.phpmailer.php');

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->Host = 'YOUR HOST';
    $mail->SMTPAuth = TRUE;

    $mail->Username = 'HOST EMAIL ACCOUNT'; 
    $mail->Password = 'HOST PASSWORD'; 

    $mail->From = 'FROM EMAIL';
    $mail->setFrom('FROM EMAIL', 'FULL NAME');

    $mail->AddAddress($to, $full_name);
    $mail->WordWrap = 70;

    $mail->Subject = $subject;
    $mail->Body = $body;

    $mail->IsHTML(TRUE);

    if(!$mail->Send()){
      echo 'SEND';
    } else {
     echo 'FAILED TO SEND';        }
}

function forgetPassword($account){
  global $dir;
  $data=getData('users',$account); //GET DATA FROM DATABASE
  $link=getDataBy('forget_password','account',$data['id']); //RELATION DATA FROM TABLE USERS
  $body='SOME TEXT <p>YOU CAN USE HTML TAG TO <a href="'.$dir.'login/changepassword?SESSION_ID='.$link['link'].'&&SESSION_VALID='.md5(rand(0,100)).'">LINK TO CLICK</a><p>END OF HTML TAG</p>';
  return $body;
}`

使用PHPMailer的PHP邮件示例,其中$to =收件人电子邮件,$full_name =收件人全名,$subject =电子邮件主题,$body =电子邮件HTML正文

lp0sw83n

lp0sw83n2#

不建议解密密码
如果理论上有可能解密你的密码,那么你就做错了。
此外,根据您的描述,如果您允许匿名更改密码,您将创建一个容易被利用的拒绝服务漏洞。正确的解决方案是:

  • 当有人Assert一个身份并请求密码重置时,生成一个具有高冗余度和有限TTL的令牌,通过电子邮件将其发送给用户。不要更改密码。
  • 当重置令牌与用户名沿着显示时,请验证TTL尚未过期,并且令牌已颁发给Assert的用户名,然后允许用户选择新密码。
cyej8jka

cyej8jka3#

<html>
  <body>
    <form method="post" action="send_link.php">
      <p>Enter Email Address To Send Password Link</p>
      <input type="text" name="email">
      <input type="submit" name="submit_email">
    </form>
  </body>
</html>

步骤1.创建一个HTML文件并定义密码重置系统的标记

<?php
if(isset($_POST['submit_email']) && $_POST['email'])
{
  mysql_connect('localhost','root','');
  mysql_select_db('sample');
  $select=mysql_query("select email,password from user where email='$email'");
  if(mysql_num_rows($select)==1)
  {
    while($row=mysql_fetch_array($select))
    {
      $email=md5($row['email']);
      $pass=md5($row['password']);
    }
    $link="<a href='www.samplewebsite.com/reset.php?key=".$email."&reset=".$pass."'>Click To Reset password</a>";
    require_once('phpmail/PHPMailerAutoload.php');
    $mail = new PHPMailer();
    $mail->CharSet =  "utf-8";
    $mail->IsSMTP();
    // enable SMTP authentication
    $mail->SMTPAuth = true;                  
    // GMAIL username
    $mail->Username = "your_email_id@gmail.com";
    // GMAIL password
    $mail->Password = "your_gmail_password";
    $mail->SMTPSecure = "ssl";  
    // sets GMAIL as the SMTP server
    $mail->Host = "smtp.gmail.com";
    // set the SMTP port for the GMAIL server
    $mail->Port = "465";
    $mail->From='your_gmail_id@gmail.com';
    $mail->FromName='your_name';
    $mail->AddAddress('reciever_email_id', 'reciever_name');
    $mail->Subject  =  'Reset Password';
    $mail->IsHTML(true);
    $mail->Body    = 'Click On This Link to Reset Password '.$pass.'';
    if($mail->Send())
    {
      echo "Check Your Email and Click on the link sent to your email";
    }
    else
    {
      echo "Mail Error - >".$mail->ErrorInfo;
    }
  } 
}
?>

步骤2.创建一个PHP文件来发送链接

<?php
if($_GET['key'] && $_GET['reset'])
{
  $email=$_GET['key'];
  $pass=$_GET['reset'];
  mysql_connect('localhost','root','');
  mysql_select_db('sample');
  $select=mysql_query("select email,password from user where md5(email)='$email' and md5(password)='$pass'");
  if(mysql_num_rows($select)==1)
  {
    ?>
    <form method="post" action="submit_new.php">
    <input type="hidden" name="email" value="<?php echo $email;?>">
    <p>Enter New password</p>
    <input type="password" name='password'>
    <input type="submit" name="submit_password">
    </form>
    <?php
  }
}
?>

步骤3.创建一个PHP文件来重置密码

<?php
if(isset($_POST['submit_password']) && $_POST['key'] && $_POST['reset'])
{
  $email=$_POST['email'];
  $pass=$_POST['password'];
  mysql_connect('localhost','root','');
  mysql_select_db('sample');
  $select=mysql_query("update user set password='$pass' where email='$email'");
}
?>

步骤4.创建一个PHP文件来更新新密码

相关问题