Advertisement
irfanamir

login2.php

Aug 31st, 2020
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.65 KB | None | 0 0
  1. <?php
  2. // Initialize the session
  3. session_start();
  4.  
  5. // Check if the user is already logged in, if yes then redirect him to welcome 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. $username = $password = "";
  16. $username_err = $password_err = "";
  17.  
  18. // Processing form data when form is submitted
  19. if($_SERVER["REQUEST_METHOD"] == "POST"){
  20.  
  21.     // Check if username is empty
  22.     if(empty(trim($_POST["username"]))){
  23.         $username_err = "Please enter username.";
  24.     } else{
  25.         $username = trim($_POST["username"]);
  26.     }
  27.    
  28.     // Check if password is empty
  29.     if(empty(trim($_POST["password"]))){
  30.         $password_err = "Please enter your password.";
  31.     } else{
  32.         $password = trim($_POST["password"]);
  33.     }
  34.    
  35.     // Validate credentials
  36.     if(empty($username_err) && empty($password_err)){
  37.         // Prepare a select statement
  38.         $sql = "SELECT id, username, password FROM users WHERE username = ?";
  39.        
  40.         if($stmt = mysqli_prepare($link, $sql)){
  41.             // Bind variables to the prepared statement as parameters
  42.             mysqli_stmt_bind_param($stmt, "s", $param_username);
  43.            
  44.             // Set parameters
  45.             $param_username = $username;
  46.            
  47.             // Attempt to execute the prepared statement
  48.             if(mysqli_stmt_execute($stmt)){
  49.                 // Store result
  50.                 mysqli_stmt_store_result($stmt);
  51.                
  52.                 // Check if username exists, if yes then verify password
  53.                 if(mysqli_stmt_num_rows($stmt) == 1){                    
  54.                     // Bind result variables
  55.                     mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password);
  56.                     if(mysqli_stmt_fetch($stmt)){
  57.                         if(password_verify($password, $hashed_password)){
  58.                             // Password is correct, so start a new session
  59.                             //session_start();
  60.                             // Store data in session variables
  61.                             $_SESSION["loggedin"] = true;
  62.                             $_SESSION["id"] = $id;
  63.                             $_SESSION["username"] = $username;                            
  64.                             // Redirect user to welcome page
  65.                             header("location: index.php");
  66.                         } else{
  67.                             // Display an error message if password is not valid
  68.                             $password_err = "The password you entered was not valid.";
  69.                         }
  70.                     }
  71.                 } else{
  72.                     // Display an error message if username doesn't exist
  73.                     $username_err = "No account found with that username.";
  74.                 }
  75.             } else{
  76.                 echo "Oops! Something went wrong. Please try again later.";
  77.             }
  78.         }
  79.        
  80.         // Close statement
  81.         mysqli_stmt_close($stmt);
  82.     }
  83.    
  84.     // Close connection
  85.     mysqli_close($link);
  86. }
  87. ?>
  88.  
  89. <!DOCTYPE html>
  90. <html lang="en">
  91. <head>
  92.     <meta charset="UTF-8">
  93.     <title>Login</title>
  94.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  95.     <style type="text/css">
  96.         body{ font: 14px sans-serif; }
  97.         .wrapper{ width: 350px; padding: 20px; }
  98.     </style>
  99. </head>
  100. <body>
  101.     <div class="wrapper">
  102.         <h2>Login</h2>
  103.         <p>Please fill in your credentials to login.</p>
  104.         <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  105.             <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  106.                 <label>Username</label>
  107.                 <input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
  108.                 <span class="help-block"><?php echo $username_err; ?></span>
  109.             </div>    
  110.             <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  111.                 <label>Password</label>
  112.                 <input type="password" name="password" class="form-control">
  113.                 <span class="help-block"><?php echo $password_err; ?></span>
  114.             </div>
  115.             <div class="form-group">
  116.                 <input type="submit" class="btn btn-primary" value="Login">
  117.             </div>
  118.             <p>Don't have an account? <a href="register.php">Sign up now</a>.</p>
  119.         </form>
  120.     </div>    
  121. </body>
  122. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement