Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. <?php
  2. ini_set("session.save_path", "/home/unn_w16037327/sessionData");
  3. session_start();
  4.  
  5. $username = filter_has_var(INPUT_POST, 'username') ? trim($_REQUEST['username']) : null;
  6. $password = filter_has_var(INPUT_POST, 'username') ? trim($_REQUEST['password']) : null;
  7.  
  8. try {
  9. unset($_SESSION['username']);
  10. require_once("functions.php");
  11. /* creating a handle called $dbConn with function from functions.php which returns a connection to our database using PDO */
  12. $dbConn = getConnection();
  13.  
  14. $sqlUser = "SELECT username, passwordHash FROM users WHERE username = :username";
  15.  
  16. $stmt = $dbConn->prepare($sqlUser);
  17. $stmt->bindValue(':username', $username, PDO::PARAM_STR);
  18. $stmt->execute();
  19.  
  20. $user = $stmt->fetchObject();
  21. if ($user) {
  22. //check that the password passed in the request stream matches with the passwordHash (after being hashed)
  23. if (password_verify($password, $user->passwordHash)) {
  24. //we're ok
  25. // set some session vars
  26. $_SESSION['username'] = $username;
  27. $_SESSION['password'] = $password;
  28. }
  29. else {
  30. //not valid password
  31. echo "<p>Invalid username or password</p>";
  32. }
  33. }
  34. else {
  35. //no user
  36. }
  37. } catch (Exception $e) {
  38. echo "<p>Query failed: ".$e->getMessage()."</p>\n";
  39. }
  40.  
  41. // do some validation
  42. if (!empty($username) AND !empty($password)) {
  43. $_SESSION['username'] = $username;
  44. $_SESSION['password'] = $password
  45. header('location: restricted.php');
  46. exit;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement