FATechID

reset-password.php

May 8th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.50 KB | None | 0 0
  1. <?php
  2. // Initialize the session
  3. session_start();
  4.  
  5. // Check if the user is logged in, if not then redirect to login page
  6. if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
  7.     header("location: index.php");
  8.     exit;
  9. }
  10.  
  11. // Include config file
  12. require_once "config.php";
  13.  
  14. // Define variables and initialize with empty values
  15. $current_password = $new_password = $confirm_password = "";
  16. $current_password_err = $new_password_err = $confirm_password_err = "";
  17.  
  18. // Processing form data when form is submitted
  19. if($_SERVER["REQUEST_METHOD"] == "POST"){
  20.  
  21.     // Validate current password
  22.     if(empty(trim($_POST["current_password"]))){
  23.         $current_password_err = "Please enter the new password.";    
  24.     } elseif(strlen(trim($_POST["current_password"])) < 8){
  25.         $current_password_err = "Password must have atleast 8 characters.";
  26.     } else{
  27.         $new_password = trim($_POST["current_password"]);
  28.     }
  29.  
  30.     // Validate new password
  31.     if(empty(trim($_POST["new_password"]))){
  32.         $new_password_err = "Please enter the new password.";    
  33.     } elseif(strlen(trim($_POST["new_password"])) < 8){
  34.         $new_password_err = "Password must have atleast 8 characters.";
  35.     } else{
  36.         $new_password = trim($_POST["new_password"]);
  37.     }
  38.    
  39.     // Validate confirm password
  40.     if(empty(trim($_POST["confirm_password"]))){
  41.         $confirm_password_err = "Please confirm the password.";
  42.     } else{
  43.         $confirm_password = trim($_POST["confirm_password"]);
  44.         if(empty($new_password_err) && ($new_password != $confirm_password)){
  45.             $confirm_password_err = "Password did not match.";
  46.         }
  47.     }
  48.  
  49.     // Check input errors before updating the database
  50.     if(empty($new_password_err) && empty($confirm_password_err)){
  51.         // Prepare an update statement
  52.         $sql = "UPDATE users SET password = ? WHERE id = ?";
  53.        
  54.         if($stmt = mysqli_prepare($link, $sql)){
  55.             // Bind variables to the prepared statement as parameters
  56.             mysqli_stmt_bind_param($stmt, "si", $param_password, $param_id);
  57.            
  58.             // Set parameters
  59.             $param_password = password_hash($new_password, PASSWORD_DEFAULT);
  60.             $param_id = $_SESSION["id"];
  61.            
  62.             // Attempt to execute the prepared statement
  63.             if(mysqli_stmt_execute($stmt)){
  64.                 // Password updated successfully. Destroy the session, and redirect to login page
  65.                 session_destroy();
  66.                 header("location: index.php");
  67.                 exit();
  68.             } else{
  69.                 echo "Oops! Something went wrong. Please try again later.";
  70.             }
  71.         }
  72.        
  73.         // Close statement
  74.         mysqli_stmt_close($stmt);
  75.     }
  76.    
  77.     // Close connection
  78.     mysqli_close($link);
  79. }
  80. ?>
  81.  
  82.  
  83. <!DOCTYPE html>
  84. <html>
  85. <head>
  86.  
  87.   <meta charset="utf-8">
  88.   <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  89.  
  90.   <title>Setting - Reset Password</title>
  91.  
  92.   <!-- Favicon -->
  93.   <link rel="icon" type="image/png" href="login-style/images/icons/favicon.ico"/>
  94.  
  95.   <!-- Fonts -->
  96.   <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet">
  97.  
  98.   <!-- Icons -->
  99.   <link href="respass-style/vendor/nucleo/css/nucleo.css" rel="stylesheet">
  100.   <link href="respass-style/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet">
  101.  
  102.   <!-- Argon CSS -->
  103.   <link type="text/css" href="respass-style/css/argon.css" rel="stylesheet">
  104. </head>
  105. <body>&nbsp;
  106.   <h2>&nbsp;&nbsp;Reset Password</h2>
  107.   <br>
  108. <div class="col-xl-8 order-xl-1">
  109.           <div class="card bg-secondary shadow">
  110.             <div class="card-header bg-white border-0">
  111.  
  112.  
  113.  
  114.         <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  115.  
  116.     <div class="col-md-6" <?php echo (!empty($new_password_err)) ? 'has-error' : ''; ?>>
  117.       <div class="form-group">
  118.         <label>New Password</label>
  119.         <input type="password" name="new_password" class="form-control form-control-alternative" id="exampleFormControlInput1" placeholder="New Password" value="<?php echo $new_password; ?>">
  120.         <i><span class="help-block"><?php echo $new_password_err; ?></span></i>
  121.       </div>
  122.     </div>
  123.  
  124. <br>
  125.  
  126.         <div class="col-md-6" <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>>
  127.       <div class="form-group">
  128.         <label>Confirm New Password</label>
  129.         <input type="password" name="confirm_password" class="form-control form-control-alternative" id="exampleFormControlInput1" placeholder="Confirm New Password" value="<?php echo $new_password; ?>">
  130.         <i><span class="help-block"><?php echo $confirm_password_err; ?></span></i>
  131.       </div>
  132.     </div>
  133.     <br/>
  134.  
  135.             <div class="form-group">
  136.                &nbsp;&nbsp;&nbsp; <input type="submit" class="btn btn-info" name="btnChangePassword" value="Submit">
  137.                 <a class="btn btn-secondary" href="welcome.php">Cancel</a>
  138.             </div>
  139.  
  140.         </form>
  141.  
  142. </div>
  143. </div>
  144. </div>
  145.  
  146.   <!-- Core -->
  147.   <script src="respass-style/vendor/jquery/jquery.min.js"></script>
  148.   <script src="respass-style/vendor/popper/popper.min.js"></script>
  149.   <script src="respass-style/vendor/bootstrap/bootstrap.min.js"></script>
  150.  
  151.   <!-- Optional plugins -->
  152.   <script src="respass-style/vendor/PLUGIN_FOLDER/PLUGIN_SCRIPT.js"></script>
  153.  
  154.   <!-- Theme JS -->
  155.   <script src="respass-style/js/argon.js"></script>
  156.   </body>
  157. </html>
Advertisement
Add Comment
Please, Sign In to add comment