Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. <?php
  2. /**
  3. * User class
  4. * Database Driver - PDO
  5. */
  6. class User
  7. {
  8. private $db; //your database handler
  9.  
  10. public function login(string $username, string $password)
  11. {
  12. // check if username exists in database
  13. $check = $this->db->query("SELECT * FROM users WHERE username = '$username' OR email = '$username')->fetchObject();
  14. if ($check) {
  15. // verify password
  16. if (password_verify($password, $check->password) {
  17. // generate your sessions here
  18. header("Location: dashboard/index");
  19. } else {
  20. // error handling
  21. }
  22. }
  23. }
  24.  
  25. }
  26.  
  27. /**
  28. * Using the class in a view
  29. */
  30.  
  31. if (isset($_POST['login']) {
  32. $user = new User; // initalize the User object
  33. $username = $_POST['username'];
  34. $password = $_POST['password'];
  35.  
  36. $user->login($username, $password);
  37. }
  38.  
  39. /**
  40. * Procedural style
  41. */
  42. .... other db crendials
  43. if (isset($_POST['login']) {
  44. $username = $_POST['username'];
  45. $password = $_POST['password']
  46.  
  47. if (!empty($username) && !empty($password)) {
  48. $check = $db->query("SELECT * FROM users WHERE username = '$username' OR email = '$username')->fetchObject();
  49. if ($check) {
  50. // verify password
  51. if (password_verify($password, $check->password) {
  52. // generate your sessions here
  53. header("Location: dashboard/index");
  54. } else {
  55. // error handling
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement