Advertisement
Guest User

Untitled

a guest
May 18th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. <?php
  2. // start session
  3. session_start();
  4.  
  5. // connect to database
  6. include 'config/db_connect.php';
  7.  
  8. // set page title
  9. $page_title="Welcome";
  10.  
  11. // page header html
  12. include 'layout_head.php';
  13.  
  14.  
  15. include 'layout_foot.php';
  16.  
  17. //home.php
  18. include 'database.php';
  19. $pdo = Database::connect();
  20.  
  21.  
  22. //If the POST var "login" exists (our submit button), then we can
  23. //assume that the user has submitted the login form.
  24. if(isset($_POST['login'])){
  25.  
  26. //Retrieve the field values from our login form.
  27. $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
  28. $passwordAttempt = !empty($_POST['password']) ? trim($_POST['password']) : null;
  29.  
  30. //Retrieve the user account information for the given username.
  31. $sql = "SELECT id, username, password FROM users WHERE username = :username";
  32. $stmt = $pdo->prepare($sql);
  33.  
  34. //Bind value.
  35. $stmt->bindValue(':username', $username);
  36.  
  37. //Execute.
  38. $stmt->execute();
  39.  
  40. //Fetch row.
  41. $user = $stmt->fetch(PDO::FETCH_ASSOC);
  42.  
  43. //If $row is FALSE.
  44. if($user === false){
  45. //Could not find a user with that username!
  46. //PS: You might want to handle this error in a more user-friendly manner!
  47. die('Incorrect username / password combination!');
  48. } else{
  49. //User account found. Check to see if the given password matches the
  50. //password hash that we stored in our users table.
  51.  
  52. //Compare the passwords.
  53. $validPassword = password_verify($passwordAttempt, $user['password']);
  54.  
  55. //If $validPassword is TRUE, the login has been successful.
  56. if($validPassword){
  57.  
  58. //Provide the user with a login session.
  59. $_SESSION['user_id'] = $user['id'];
  60. $_SESSION['logged_in'] = time();
  61.  
  62. //Redirect to our protected page, which we called home.php
  63. header('Location: products.php');
  64. exit;
  65.  
  66. } else{
  67. //$validPassword was FALSE. Passwords do not match.
  68. die('Incorrect username / password combination!');
  69. }
  70. }
  71.  
  72. }
  73. Database::disconnect();
  74. ?>
  75. <!DOCTYPE html>
  76. <html>
  77. <head>
  78. <meta charset="UTF-8">
  79. <title>Login</title>
  80. </head>
  81. <body>
  82. <h3 align="center">Thank you for visiting the ScotBooks ecommerce website.</h3>
  83.  
  84. <form action="login.php" method="post">
  85. <table border="0" cellpadding="10" cellspacing="1" width="500" align="center">
  86. <tr class="tableheader">
  87. <td align="center" colspan="2">Please enter your login details</td>
  88. </tr>
  89. <tr class="tablerow">
  90.  
  91. <td align="center"><label for="username">Username</label>
  92. <input type="text" id="username" name="username"><br></td></tr>
  93. <tr class="tablerow">
  94. <td align="center"><label for="password">Password</label>
  95. <input type="password" id="password" name="password"><br></td></tr>
  96. <tr class="tablerow">
  97. <td align="center"><input type="submit" name="login" value="Login"></td>
  98. </tr>
  99. </table>
  100. <br></br>
  101. <p align="center">If you do not have an active account, please register <a href="register.php">Here</a></p>
  102. </form>
  103. </body>
  104. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement