Advertisement
Guest User

Untitled

a guest
Mar 25th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. <?php
  2.  
  3. require_once 'Database.class.php';
  4.  
  5. class Auth extends Database
  6. {
  7. const DATEFORMAT = 'd/m/Y';
  8. const TIMEFORMAT = 'H:i:s';
  9.  
  10. public function __construct($host, $username, $password, $database)
  11. {
  12. parent::__construct($host, $username, $password, $database);
  13. $this->connect();
  14. $this->setTable('users');
  15. }
  16.  
  17. public function insert(array $data)
  18. {
  19. $username = $data['username'];
  20. $password = $data['password'];
  21. $repeat_password = $data['repeat_password'];
  22. $last_login = date(self::DATEFORMAT . ' ' . self::TIMEFORMAT);
  23.  
  24. if ($password == $repeat_password)
  25. {
  26. $password = hash("sha256", $password);
  27. $stmt = $this->connection->prepare("INSERT INTO {$this->table} (username, password, last_login) VALUES(:username, :password, :last_login)");
  28.  
  29. $stmt->execute(array(':username' => $username,
  30. ':password' => $password,
  31. ':last_login' => $last_login
  32. )
  33. );
  34. }
  35. else
  36. {
  37. // do something
  38. }
  39. }
  40.  
  41. public function select(array $data)
  42. {
  43. $username = $data['username'];
  44. $password = $data['password'];
  45. $password = hash("sha256", $password);
  46.  
  47. $stmt = $this->connection->prepare("SELECT * FROM {$this->table} WHERE username=:username AND password=:password LIMIT 1");
  48.  
  49. $stmt->execute(array(':username' => $username,
  50. ':password' => $password
  51. )
  52. );
  53.  
  54. return $stmt->fetch(PDO::FETCH_ASSOC);
  55. }
  56.  
  57. }
  58.  
  59. <?php
  60.  
  61. require_once 'Auth.class.php';
  62.  
  63. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  64.  
  65. $auth = new Auth('localhost', 'root', '', 'oop');
  66.  
  67. $user = $auth->select($_POST);
  68.  
  69. if (count($user) === 0) {
  70. echo 'User not found!';
  71. }else{
  72. echo 'Hello, ' . ucfirst($user['username']);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement