Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.76 KB | None | 0 0
  1. <?php
  2. // Include config file
  3. require_once 'config.php';
  4.  
  5. // Define variables and initialize with empty values
  6. $username = $password = "";
  7. $failed_logins = 0;
  8. $username_err = $password_err = "";
  9.  
  10. // Processing form data when form is submitted
  11. if($_SERVER["REQUEST_METHOD"] == "POST"){
  12.   //basic CSRF protection, verifying http origin header
  13.   #if (isset($_SERVER["HTTP_ORIGIN"])) {
  14.  #      $address = "https://".$_SERVER["SERVER_NAME"];
  15.  #      if (strpos($address, $_SERVER["HTTP_ORIGIN"]) !== 0) {
  16.  #          exit("CSRF protection in POST request: detected invalid Origin header: ".$_SERVER["HTTP_ORIGIN"]);
  17.  #      }
  18.  #  }
  19.    // Check if username is empty
  20.     if(empty(trim($_POST["username"]))){
  21.         $username_err = 'Please enter username.';
  22.     } else{
  23.         $username = trim($_POST["username"]);
  24.     }
  25.  
  26.     // Check if password is empty
  27.     if(empty(trim($_POST['password']))){
  28.         $password_err = 'Please enter your password.';
  29.     } else{
  30.         $password = trim($_POST['password']);
  31.     }
  32.     #echo $username;
  33.    #echo $password;
  34.    $sql = "SELECT username, password, failed_logins FROM users WHERE username = $username";
  35.     $row = mysqli_query($link, $sql);
  36.     echo $row;
  37.  
  38.  
  39.   /*
  40.     // Validate credentials
  41.     if(empty($username_err) && empty($password_err)){
  42.         // Prepare a select statement
  43.         $sql = "SELECT username, password, failed_logins FROM users WHERE username = ?";
  44.  
  45.         if($stmt = mysqli_prepare($link, $sql)){
  46.             // Bind variables to the prepared statement as parameters
  47.             mysqli_stmt_bind_param($stmt, "s", $param_username);
  48.  
  49.             // Set parameters
  50.             $param_username = $username;
  51.  
  52.             // Attempt to execute the prepared statement
  53.             if(mysqli_stmt_execute($stmt)){
  54.                 // Store result
  55.                 mysqli_stmt_store_result($stmt);
  56.  
  57.                 // Check if username exists, if yes then verify password
  58.                 if(mysqli_stmt_num_rows($stmt) == 1){
  59.                     // Bind result variables
  60.                     mysqli_stmt_bind_result($stmt, $username, $hashed_password, $failed_logins);
  61.                     if(mysqli_stmt_fetch($stmt)){
  62.                         if($failed_logins > 4) {
  63.                             echo "You have too many failed attempts and your account is now locked";
  64.                         } elseif(password_verify($password, $hashed_password)){
  65.                             #Password is correct, so start a new session and
  66.                             #save the username to the session. Failed logins is set to 0
  67.                             $sql2 = "UPDATE users SET failed_logins=? WHERE username=?";
  68.                             if($stmt2 = mysqli_prepare($link, $sql2)){
  69.                                 // Bind variables to the prepared statement as parameters
  70.                                 mysqli_stmt_bind_param($stmt2, "is", $param_failed, $param_username);
  71.                                 // Set parameters
  72.                                 $param_failed = 0;
  73.                                 // Attempt to execute the prepared statement
  74.                                 if(mysqli_stmt_execute($stmt2)){
  75.                                 // Redirect to login page
  76.                                 header("location: login.php");
  77.                                 } else{
  78.                                  echo "Something went wrong. Please try again later.";
  79.                                 }
  80.                             }
  81.                             // Close statement
  82.                             mysqli_stmt_close($stmt2);
  83.                             session_start();
  84.                             $_SESSION['username'] = $username;
  85.                             header("location: welcome.php");
  86.                         } else{
  87.                             // Display an error message if password is not valid
  88.                             $sql2 = "UPDATE users SET failed_logins=? WHERE username=?";
  89.                             if($stmt2 = mysqli_prepare($link, $sql2)){
  90.                                 // Bind variables to the prepared statement as parameters
  91.                                 mysqli_stmt_bind_param($stmt2, "is", $param_failed, $param_username);
  92.                                 // Set parameters
  93.                                 $param_failed = $failed_logins + 1;
  94.                                 // Attempt to execute the prepared statement
  95.                                 if(!mysqli_stmt_execute($stmt2)){
  96.                                  echo "Something went wrong. Please try again later.";
  97.                                 }
  98.                             }
  99.                             // Close statement
  100.                             mysqli_stmt_close($stmt2);
  101.                             $password_err = 'The password you entered was not valid.';
  102.                         }
  103.                     }
  104.                 } else{
  105.                     // Display an error message if username doesn't exist
  106.                     $username_err = 'No account found with that username.';
  107.                 }
  108.             } else{
  109.                 echo "Oops! Something went wrong. Please try again later.";
  110.             }
  111.         }
  112.  
  113.         // Close statement
  114.         mysqli_stmt_close($stmt);
  115.     }
  116. */
  117.     // Close connection
  118.     mysqli_close($link);
  119. }
  120. ?>
  121.  
  122. <!DOCTYPE html>
  123. <html lang="en">
  124. <head>
  125.     <meta charset="UTF-8">
  126.     <title>Login</title>
  127.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  128.     <style type="text/css">
  129.         body{ font: 14px sans-serif; }
  130.         .wrapper{ width: 350px; padding: 20px; }
  131.     </style>
  132. </head>
  133. <body>
  134.     <div class="wrapper">
  135.         <h2>SQLogin</h2>
  136.         <p>Please fill in your credentials to login.</p>
  137.         <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  138.             <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  139.                 <label>Username:<sup>*</sup></label>
  140.                 <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
  141.                 <span class="help-block"><?php echo $username_err; ?></span>
  142.             </div>
  143.             <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  144.                 <label>Password:<sup>*</sup></label>
  145.                 <input type="password" name="password" class="form-control">
  146.                 <span class="help-block"><?php echo $password_err; ?></span>
  147.             </div>
  148.             <div class="form-group">
  149.                 <input type="submit" class="btn btn-primary" value="Submit">
  150.             </div>
  151.             <p>Don't have an account? <a href="register.php">Sign up now</a>.</p>
  152.         </form>
  153.     </div>
  154. </body>
  155. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement