Guest User

Untitled

a guest
Sep 23rd, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. <!-- Form -->
  2. <form class="text-center formulario" style="color: #757575;" action="recuperar_pass.php" method="POST">
  3.  
  4. <!-- Email -->
  5. <div class="md-form">
  6. <input type="email" id="materialLoginFormEmail" class="form-control" name="email">
  7. <label for="materialLoginFormEmail">E-mail</label>
  8. </div>
  9.  
  10. <!-- Sign in button -->
  11. <div class="text-center mt-4">
  12. <button class="btn btn-warning btn-lg mt-4" value="enviar">Enviar</button>
  13. </div>
  14.  
  15. </form>
  16.  
  17. <?php
  18. use PHPMailerPHPMailerPHPMailer;
  19. use PHPMailerPHPMailerException;
  20.  
  21. require 'vendor/autoload.php';
  22.  
  23.  
  24.  
  25. $link = 'mysql:host=localhost;dbname=drivers_parade_club';
  26. $usuario = 'root';
  27. $contraseña = '';
  28.  
  29. if (array_key_exists('email', $_POST) ) {
  30. try{
  31. $pdo = new PDO($link,$usuario,$contraseña);
  32. $sql = "SELECT email FROM usuarios";
  33. $st = $pdo-> prepare($sql);
  34. $st->bindValue(1,$_POST['email']);
  35. $st->execute();
  36. if ($resultado = $st->fetch(PDO::FETCH_ASSOC)){
  37. echo 'Hemos enviado un mail a '.$resultado['email'];
  38. $token = uniqid();
  39. $sql = "UPDATE usuarios SET token = '$token' WHERE email = '{$resultado['email']}'";
  40. try{
  41. $pdo->exec($sql);
  42. $mail = new PHPMailer(true); // Passing `true` enables exceptions
  43. try {
  44. //Server settings
  45. $mail->SMTPDebug = 2; // Enable verbose debug output
  46. $mail->isSMTP(); // Set mailer to use SMTP
  47. $mail->Host = 'smtp.gmail.com;'; // Specify main and backup SMTP servers
  48. $mail->SMTPAuth = true; // Enable SMTP authentication
  49. $mail->Username = 'user@gmail.com'; // SMTP username
  50. $mail->Password = 'secret'; // SMTP password
  51. $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
  52. $mail->Port = 587; // TCP port to connect to
  53.  
  54. //Recipients
  55. $mail->setFrom('driversparadeclub@gmail.com', 'Drivers Parade Club');
  56.  
  57.  
  58. //Content
  59. $mail->isHTML(true); // Set email format to HTML
  60. $mail->Subject = 'Recupere su clave';
  61. $mail->Body = 'Haga click en <a href="https://www.driversparadeclub.org/php/recuperar.php?token='.$token.'">este link</a>';
  62. $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  63.  
  64. $mail->send();
  65. echo 'Message has been sent';
  66. } catch (Exception $e) {
  67. echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
  68. }
  69. }catch (PDOException $e){
  70. echo 'No se ha podido guardar el token: '.$e->getMessage();
  71. }
  72.  
  73. } else{
  74. echo 'No existe el usuario';
  75.  
  76. }
  77. }catch(PDOException $e){
  78. echo 'Fallo en la conexión a la base: '.$e->getMessage();
  79. }
  80. }
  81.  
  82. <?php
  83. $link = 'mysql:host=localhost;dbname=drivers_parade_club';
  84. $usuario = 'root';
  85. $contraseña = '';
  86. $pdo = new PDO($link,$usuario,$contraseña);
  87.  
  88. if (array_key_exists($_GET['token'])){
  89. $sql ="SELECT id, username FROM usuarios WHERE token = ?";
  90. $st = $pdo->prepare($sql);
  91. $st->bindValue (1,$_GET['token']);
  92. $st->execute();
  93. if($resultado = $st->fetch(PDO::FETCH_ASSOC)){
  94. $sql = "UPDATE usuarios SET token = null WHERE id = {$resultado['id']}";
  95. $pdo->exec($sql);
  96. ?>
  97. <h1>Bienvenido <?php echo $resultado['email'];?></h1>
  98. <form action="recuperar.php" method="post">
  99. <input type= "hidden" value= "<?php echo $resultado['id'];?>" name="id"/>
  100. <table>
  101. <tr>
  102. <td><label for="newPassword">Ingresa tu nueva contraseña</label></td>
  103. <td><input type="password" id="newPassword" name="newPassword"/>
  104. </tr>
  105. <tr>
  106. <td colspan="2">
  107. <input type="submit" value="Enviar"/>
  108. </td>
  109. </tr>
  110. </table>
  111. </form>
  112. <?php
  113. }
  114. }elseif (array_key_exists('id',$_POST)){
  115.  
  116. $sql = "UPDATE usuarios SET pass = ".hash('sha512',$_POST['newPassowrd'])." WHERE id ={$resultado['id']}";
  117. $pdo->exec($sql);
  118.  
  119. echo 'Contraseña cambiada';
  120. }
Add Comment
Please, Sign In to add comment