Advertisement
Guest User

Untitled

a guest
Nov 26th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. <?php
  2. session_start();
  3.  
  4. //include ircmaxell's password_compat library
  5. require 'lib/password.php';
  6.  
  7. //include MySQL conn
  8. require 'phpscripts/connect.php';
  9. $database = new DB;
  10. $database->dbConn();
  11.  
  12. class Login {
  13.  
  14. private $errors = array();
  15.  
  16. function login() {
  17.  
  18. if (isset($_POST['login'])) {
  19. $username = !empty($_POST['username']) ? trim($_POST['username']) : null;
  20. $password = !empty($_POST['password']) ? trim($_POST['password']) : null;
  21.  
  22. //retrive the user information for the username
  23. $sql = "SELECT id, username, password FROM users WHERE username = :username";
  24. $stmt = $database->db->prepare($sql);
  25.  
  26. $stmt->bindValue(':username', $username);
  27. $stmt->execute();
  28.  
  29. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  30.  
  31. if($result !== FALSE) {
  32. //compare the passwords
  33. $validPassword = password_verify($password, $result['password']);
  34.  
  35. if ($validPassword == TRUE) {
  36. $_SESSION['user_id'] = $result['id'];
  37. $_SESSION['user'] = $result['username'];
  38. $_SESSION['now_logged'] = TRUE;
  39. $_SESSION['logged_in'] = time();
  40.  
  41. header('Location: index.php');
  42.  
  43.  
  44. }
  45. else {
  46. //passwords don't match
  47. $stmt = NULL;
  48. $db = NULL;
  49. $result = NULL;
  50.  
  51. array_push($this->errors, "Invalid password!");
  52. }
  53. }
  54. else {
  55. $stmt = NULL;
  56. $db = NULL;
  57. $result = NULL;
  58.  
  59. array_push($this->errors, "Invalid username!");
  60. }
  61. }
  62.  
  63. }
  64. }
  65. $login = new Login;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement