Guest User

Untitled

a guest
Sep 15th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. class UserService
  2. {
  3. protected $_email; // using protected so they can be accessed
  4. protected $_password; // and overidden if necessary
  5.  
  6. protected $_db; // stores the database handler
  7. protected $_user; // stores the user data
  8.  
  9. public function __construct(PDO $db, $email, $password)
  10. {
  11. $this->_db = $db;
  12. $this->_email = $email;
  13. $this->_password = $password;
  14. }
  15.  
  16. public function login()
  17. {
  18. $user = $this->_checkCredentials();
  19. if ($user) {
  20. $this->_user = $user; // store it so it can be accessed later
  21. $_SESSION['user_id'] = $user['id'];
  22. return $user['id'];
  23. }
  24. return false;
  25. }
  26.  
  27. protected function _checkCredentials()
  28. {
  29. $stmt = $this->_db->prepare('SELECT * FROM users WHERE email=?');
  30. $stmt->execute(array($this->email));
  31. if ($stmt->rowCount() > 0) {
  32. $user = $stmt->fetch(PDO::FETCH_ASSOC);
  33. $submitted_pass = sha1($user['salt'] . $this->_password);
  34. if ($submitted_pass == $user['password']) {
  35. return $user;
  36. }
  37. }
  38. return false;
  39. }
  40.  
  41. public function getUser()
  42. {
  43. return $this->_user;
  44. }
  45. }
Add Comment
Please, Sign In to add comment