Guest User

Untitled

a guest
Sep 15th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1.  <?php
  2.  include('includes/functions.php');
  3.  if(isset($_POST['login'])) {
  4.  
  5.   //create variables
  6.   //wrap the data with our function
  7.   $formUser = validateFormData($_POST['username']);
  8.   $formPass = validateFormData($_POST['password']);
  9.  
  10.   // connect to database
  11.   include('includes/connection.php');
  12.  
  13.   //create SQL query
  14.   $query = "SELECT username, email, password FROM users WHERE username='$formUser'";
  15.  
  16.   // store the result
  17.   $result = mysqli_query($conn, $query);
  18.  
  19.   // verify if result is returned
  20.   if (mysqli_num_rows($result) > 0 ) {
  21.     // store basic user data in variables
  22.     while ($row = mysqli_fetch_assoc($result)) {
  23.       $user = $row["username"];
  24.       $email = $row["email"];
  25.       $hashedPass = $row["password"];
  26.   }
  27.  
  28.   // verify hashed password with the typed password
  29.   if (password_verify($formPass, $hashedPass)) {
  30.  
  31.     // correct login details!
  32.     // start the Session
  33.     session_start();
  34.  
  35.     //store data in session variables
  36.     $_SESSION['loggedInUser'] = $user;
  37.     $_SESSION['loggedInEmail'] = $email;
  38.  
  39.     //Even though there aren't any errors, we're gonna initialize the variable
  40.     $loginError = "";
  41.  
  42.     header("Location: checker.php");
  43.   } else {
  44.     // error message
  45.     $loginError = "<div class='alert alert-danger'>Please check your username or password.</div>";
  46.   }
  47.  
  48.   }else {
  49.     // there are no results in database
  50.     $loginError = "<div class='alert alert-danger'>No user in the database. <a class='close' data-dismiss='alert'>&times</a></div>";
  51.   }
  52.  
  53.   // close mysql connection
  54.   mysqli_close($conn);
  55.  
  56.  }
  57.  
  58.  ?>
  59.  
  60.  
  61.  <!DOCTYPE html>
  62.  <html lang="en">
  63.  <head>
  64.    <title>Login</title>
  65.    <meta charset="utf-8">
  66.    <meta name="viewport" content="width=device-width, initial-scale=1">
  67.    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  68.    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  69.    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  70.  </head>
  71.  <body>
  72.  
  73.  <div class="container">
  74.    <h1>Login</h1>
  75.    <p class="lead">Log in to your account!</p>
  76.  
  77.    <?php echo $loginError ?>
  78.  
  79.    <form class="" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'] ); ?>" method="post">
  80.  <div class="form-group">
  81.      <label>* Username</label>
  82.      <input type="text" name="username" value="Username" class="form-control" id="login-username" placeholder="username">
  83.  </div>
  84.  <div class="form-group">
  85.      <label>* Password</label>
  86.      <input type="password" name="password" value="Password" class="form-control" id="login-password" placeholder="password">
  87.  </div>
  88.  <button type="submit" class="btn btn-default" name="login">Login</button>
  89.  </form>
  90.  </div>
  91.  
  92.  </body>
  93.  </html>
Advertisement
Add Comment
Please, Sign In to add comment