Advertisement
Guest User

Untitled

a guest
Sep 29th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 KB | None | 0 0
  1. <?php
  2. // include shared code
  3. include 'lib/common.php';
  4. include 'lib/db.php';
  5. include 'lib/functions.php';
  6. include 'lib/User.php';
  7.  
  8. // construct password request form HTML
  9. ob_start();
  10. ?>
  11. <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"
  12. method="post">
  13. <p>Enter your username. A new password will be sent to the email address on file.</p>
  14. <table>
  15.     <tr>
  16.         <td><label for="username">Username</label></td>
  17.         <td><input type="text" name="username" id="username"
  18.         value="<?php if (isset($_POST['username']))
  19.         echo htmlspecialchars($_POST['username']); ?>"/></td>
  20.      </tr>
  21.      <tr>
  22.         <td></td>
  23.         <td><input type="submit" value="Submit"/></td>
  24.         <td><input type="hidden" name="submitted" value="1"/></td>
  25.      </tr>
  26. </table>
  27. </form>
  28. <?php
  29. $form = ob_get_clean();
  30.  
  31. // show the form if this is the first time the page is viewed
  32. if (!isset($_POST['submitted']))
  33. {
  34.     $GLOBALS['TEMPLATE']['content'] = $form;
  35. }
  36. // otherwise process incoming data
  37. else
  38. {
  39.     // validate username
  40.     if (User::validateUsername($_POST['username']))
  41.     {
  42.         $user = User::getByUsername($_POST['username']);
  43.         if (!user->userId)
  44.         {
  45.             $GLOBALS['TEMPLATE']['content'] = '<p><strong>Sorry, that ' .
  46.                 'account does not exist.</strong></p> <p>Please try a ' .
  47.                 'different username.</p>';
  48.             $GLOBALS['TEMPLATE']['content'] .= $form;
  49.         }
  50.         else
  51.         {
  52.             // generate new password
  53.             $password = random_text(8);
  54.            
  55.             // send the new password to the email address on record
  56.             $message = 'Your new password is: ' . $password;
  57.             mail($user->emailAddr, 'New password', $message);
  58.            
  59.             $GLOBALS['TEMPLATE']['content'] = '<p><strong>A new ' .
  60.                 'password has been emailed to you.</strong></p>';
  61.                
  62.             // store the new password
  63.             $user->password = $password;
  64.             $user->save();
  65.         }
  66.     }
  67.     // there was invalid data
  68.     else
  69.     {
  70.         $GLOBALS['TEMPLATE']['content'] .= '<p><strong>You did not ' .
  71.             'provide a valid username.</strong></p> <p>Please try ' .
  72.             'again.</p>';
  73.         $GLOBALS['TEMPLATE']['content'] .= $form;
  74.     }
  75. }
  76.  
  77. // display the page
  78. include 'templates/template-page.php';
  79. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement