Advertisement
lamorfini

Untitled

Aug 12th, 2024
100
0
261 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.36 KB | None | 0 0
  1.  public function requestPasswordReset($email)
  2.     {
  3.         // Check if the email exists
  4.         $sql = "SELECT * FROM users WHERE email = :email";
  5.         $stmt = $this->pdo->prepare($sql);
  6.         $stmt->bindParam(':email', $email);
  7.         $stmt->execute();
  8.         $user = $stmt->fetch();
  9.  
  10.         if ($user) {
  11.             // Generate a reset token and expiry
  12.             $token = bin2hex(random_bytes(16));
  13.             $expiry = new DateTime('+1 hour');  // Token valid for 1 hour
  14.             // Update the user with the reset token and expiry
  15.             $sql = "UPDATE users SET reset_token = :token, reset_token_expiry = :expiry WHERE email = :email";
  16.             $stmt = $this->pdo->prepare($sql);
  17.             $stmt->bindParam(':token', $token);
  18.             $stmt->bindParam(':expiry', $expiry->format('Y-m-d H:i:s')); <!-- Only variables should be passed by reference
  19.             $stmt->bindParam(':email', $email);
  20.             $stmt->execute();
  21.             // Send email with reset link (replace with your actual domain)
  22.             $resetLink = "https://www.luigiamorfini.local/accounts/reset_password.php?token=$token";
  23.             mail($email, 'Password Reset Request', "Click the link to reset your password: $resetLink");
  24.  
  25.             return 'A password reset link has been sent to your email.';
  26.         }
  27.         return 'Email not found.';
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement