Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Include config file
- require_once 'config.php';
- // Define variables and initialize with empty values
- $username = $password = "";
- $failed_logins = 0;
- $username_err = $password_err = "";
- // Processing form data when form is submitted
- if($_SERVER["REQUEST_METHOD"] == "POST"){
- //basic CSRF protection, verifying http origin header
- #if (isset($_SERVER["HTTP_ORIGIN"])) {
- # $address = "https://".$_SERVER["SERVER_NAME"];
- # if (strpos($address, $_SERVER["HTTP_ORIGIN"]) !== 0) {
- # exit("CSRF protection in POST request: detected invalid Origin header: ".$_SERVER["HTTP_ORIGIN"]);
- # }
- # }
- // Check if username is empty
- if(empty(trim($_POST["username"]))){
- $username_err = 'Please enter username.';
- } else{
- $username = trim($_POST["username"]);
- }
- // Check if password is empty
- if(empty(trim($_POST['password']))){
- $password_err = 'Please enter your password.';
- } else{
- $password = trim($_POST['password']);
- }
- #echo $username;
- #echo $password;
- $sql = "SELECT username, password, failed_logins FROM users WHERE username = $username";
- $row = mysqli_query($link, $sql);
- echo $row;
- /*
- // Validate credentials
- if(empty($username_err) && empty($password_err)){
- // Prepare a select statement
- $sql = "SELECT username, password, failed_logins FROM users WHERE username = ?";
- if($stmt = mysqli_prepare($link, $sql)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt, "s", $param_username);
- // Set parameters
- $param_username = $username;
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt)){
- // Store result
- mysqli_stmt_store_result($stmt);
- // Check if username exists, if yes then verify password
- if(mysqli_stmt_num_rows($stmt) == 1){
- // Bind result variables
- mysqli_stmt_bind_result($stmt, $username, $hashed_password, $failed_logins);
- if(mysqli_stmt_fetch($stmt)){
- if($failed_logins > 4) {
- echo "You have too many failed attempts and your account is now locked";
- } elseif(password_verify($password, $hashed_password)){
- #Password is correct, so start a new session and
- #save the username to the session. Failed logins is set to 0
- $sql2 = "UPDATE users SET failed_logins=? WHERE username=?";
- if($stmt2 = mysqli_prepare($link, $sql2)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt2, "is", $param_failed, $param_username);
- // Set parameters
- $param_failed = 0;
- // Attempt to execute the prepared statement
- if(mysqli_stmt_execute($stmt2)){
- // Redirect to login page
- header("location: login.php");
- } else{
- echo "Something went wrong. Please try again later.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt2);
- session_start();
- $_SESSION['username'] = $username;
- header("location: welcome.php");
- } else{
- // Display an error message if password is not valid
- $sql2 = "UPDATE users SET failed_logins=? WHERE username=?";
- if($stmt2 = mysqli_prepare($link, $sql2)){
- // Bind variables to the prepared statement as parameters
- mysqli_stmt_bind_param($stmt2, "is", $param_failed, $param_username);
- // Set parameters
- $param_failed = $failed_logins + 1;
- // Attempt to execute the prepared statement
- if(!mysqli_stmt_execute($stmt2)){
- echo "Something went wrong. Please try again later.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt2);
- $password_err = 'The password you entered was not valid.';
- }
- }
- } else{
- // Display an error message if username doesn't exist
- $username_err = 'No account found with that username.';
- }
- } else{
- echo "Oops! Something went wrong. Please try again later.";
- }
- }
- // Close statement
- mysqli_stmt_close($stmt);
- }
- */
- // Close connection
- mysqli_close($link);
- }
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Login</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
- <style type="text/css">
- body{ font: 14px sans-serif; }
- .wrapper{ width: 350px; padding: 20px; }
- </style>
- </head>
- <body>
- <div class="wrapper">
- <h2>SQLogin</h2>
- <p>Please fill in your credentials to login.</p>
- <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
- <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
- <label>Username:<sup>*</sup></label>
- <input type="text" name="username"class="form-control" value="<?php echo $username; ?>">
- <span class="help-block"><?php echo $username_err; ?></span>
- </div>
- <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
- <label>Password:<sup>*</sup></label>
- <input type="password" name="password" class="form-control">
- <span class="help-block"><?php echo $password_err; ?></span>
- </div>
- <div class="form-group">
- <input type="submit" class="btn btn-primary" value="Submit">
- </div>
- <p>Don't have an account? <a href="register.php">Sign up now</a>.</p>
- </form>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement