Advertisement
Guest User

Untitled

a guest
Nov 24th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. <?php
  2. class Login implements Authenticable {
  3. private $user;
  4. private $password;
  5.  
  6. function __construct(array $attributes) {
  7. if (!isset($atributes['user'])) {
  8. throw new LoginException('User name expected');
  9. }
  10. $this->user = $attributes['user'];
  11. $this->password = isset($attributes['password']) ? $attributes['password'] : '';
  12. }
  13.  
  14. function authenticate(MyPDO $db) {
  15. try {
  16. $sql = "SELECT id, name, password, surname FROM user WHERE nickname=?";
  17. $select = $db->prepare($sql);
  18. $select->execute(array($this->user));
  19. $profile = $select->fetch(\PDO::FETCH_OBJ);
  20. if (($profile === false) ||
  21. (crypt($this->password, $profile->password) !== $profile->password)) {
  22. throw new LoginException('User not found');
  23. }
  24. $_SESSION['id'] = $profile->id;
  25. $_SESSION['name'] = $profile->name;
  26. $_SESSION['surname'] = $profile->surname;
  27. $_SESSION['error'] = array();
  28. } catch (\PDOException $e) {
  29. throw new LoginException(__METHOD__ . $e->getMessage());
  30. }
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement