Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. require_once 'config.php';
  5.  
  6. /**
  7. * Include ircmaxell's password_compat library.
  8. */
  9. require '../lib/password.php';
  10.  
  11. if(isset($_POST['submit'])){
  12. //Retrieve the field values from our login form.
  13. $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
  14. $passwordAttempt = !empty($_POST['password']) ? trim($_POST['password']) : null;
  15.  
  16. try {
  17. $sql = "SELECT id, username, password FROM users_table WHERE username = :username";
  18. $stmt = $pdo->prepare($sql);
  19.  
  20. //Bind value.
  21. $stmt->bindValue(':username', $username);
  22.  
  23. //Execute.
  24. $stmt->execute();
  25.  
  26. //Fetch row.
  27. $user = $stmt->fetch(PDO::FETCH_ASSOC);
  28.  
  29. //If $row is FALSE.
  30. if($user === false){
  31. //Could not find a user with that username!
  32. //PS: You might want to handle this error in a more user-friendly manner!
  33. echo '<script>alert("Incorrect username!");</script>';
  34. header("Refresh: 0; URL = ../signin.php");
  35. die();
  36. } else{
  37. //User account found. Check to see if the given password matches the
  38. //password hash that we stored in our users table.
  39.  
  40. //Compare the passwords.
  41. $validPassword = password_verify($passwordAttempt, $user['password']);
  42.  
  43. //If $validPassword is TRUE, the login has been successful.
  44. if($validPassword){
  45. //Provide the user with a login session.
  46. //$row = $sql->fetch($sql);
  47. session_start();
  48. $_SESSION['username'] = $user['username'];
  49. $_SESSION['fname'] = $user['first_name'];
  50. $_SESSION['lname'] = $user['last_name'];
  51. $_SESSION['logged'] = TRUE;
  52. header("Location: ../index.php"); // Success!
  53. exit;
  54.  
  55. } else{
  56. //$validPassword was FALSE. Passwords do not match.
  57. echo '<script>alert("Incorrect password!");</script>';
  58. header("Refresh: 0; URL = ../signin.php");
  59. die();
  60. }
  61. }
  62. }
  63. catch(PDOException $e)
  64. {
  65. die( print_r( $e->getMessage() ) );
  66. }
  67.  
  68. }else{
  69. header("Location: ../index.php");
  70. exit;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement