Advertisement
Guest User

Untitled

a guest
May 20th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. <?php
  2. //login.php
  3.  
  4. /**
  5. * Start the session.
  6. */
  7. session_start();
  8.  
  9.  
  10. include 'database.php';
  11. $pdo = Database::connect();
  12.  
  13. //If the POST var "login" exists (our submit button), then we can
  14. //assume that the user has submitted the login form.
  15. if(isset($_POST['login'])){
  16.  
  17. //Retrieve the field values from our login form.
  18. $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
  19. $passwordAttempt = !empty($_POST['password']) ? trim($_POST['password']) : null;
  20.  
  21. //Retrieve the user account information for the given username.
  22. $sql = "SELECT id, username, password FROM users WHERE username = :username";
  23. $stmt = $pdo->prepare($sql);
  24.  
  25. //Bind value.
  26. $stmt->bindValue(':username', $username);
  27.  
  28. //Execute.
  29. $stmt->execute();
  30.  
  31. //Fetch row.
  32. $user = $stmt->fetch(PDO::FETCH_ASSOC);
  33.  
  34. //If $row is FALSE.
  35. if($user === false){
  36. //Could not find a user with that username!
  37. //PS: You might want to handle this error in a more user-friendly manner!
  38. die('Incorrect username / password combination! Click <a href="login.php">here</a> to go back.');
  39. } else{
  40. //User account found. Check to see if the given password matches the
  41. //password hash that we stored in our users table.
  42.  
  43. //Compare the passwords.
  44. $validPassword = password_verify($passwordAttempt, $user['password']);
  45.  
  46. //If $validPassword is TRUE, the login has been successful.
  47. if($validPassword){
  48.  
  49. //Provide the user with a login session.
  50. //$_SESSION['user_id'] = $user['id'];
  51. $_SESSION['username'] = $user['username'];
  52. $_SESSION['logged_in'] = time();
  53.  
  54. //Redirect to our protected page, which we called adminpage.php
  55. header('Location: adminpage.php');
  56. exit;
  57.  
  58. } else{
  59. //$validPassword was FALSE. Passwords do not match.
  60. die('Incorrect username / password combination!');
  61. }
  62. }
  63.  
  64. }
  65. Database::disconnect();
  66. ?>
  67. <!DOCTYPE html>
  68. <html>
  69. <head>
  70. <meta charset="UTF-8">
  71. <link href="css/bootstrap.min.css" rel="stylesheet">
  72. <title>Admin Login</title>
  73. </head>
  74. <body>
  75. <h1>Admin Login</h1>
  76. <form action="login.php" method="post">
  77. <label for="username">Username</label>
  78. <input type="text" id="username" name="username"><br>
  79. <label for="password">Password</label>
  80. <input type="password" id="password" name="password"><br>
  81. <input type="submit" name="login" value="Login">
  82. </form>
  83. </body>
  84.  
  85. <body>
  86. <h2>If you are a customer click this button.<h2>
  87. <form action="customerpage.php" method="post">
  88. <input type="submit" name="" value="Customer Page">
  89. </form>
  90. </body>
  91. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement