Advertisement
Guest User

Untitled

a guest
Nov 5th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. <?php
  2.  
  3. // Session Start
  4.  
  5. session_name('client');
  6. session_start();
  7.  
  8. // Config MySQL
  9.  
  10. require_once('DB.config.php');
  11.  
  12. $PDO = (new DB())->pdo;
  13.  
  14. // Function Logged
  15.  
  16. $logged = NULL;
  17. if(!empty($_SESSION['logged'])) {
  18. $logged = $_SESSION['logged'];
  19. }
  20.  
  21. // Function login
  22.  
  23. class User {
  24.  
  25. public function __construct() {
  26. global $PDO;
  27. if(!empty($_SESSION['uid']) && !empty($_SESSION['user'])) {
  28. $this->userData = $PDO->prepare('SELECT * FROM servers WHERE id=:id LIMIT 1');
  29. $this->userData->bindValue('id', $_SESSION['uid']);
  30. $this->userData->execute();
  31. $this->userData = $this->userData->fetchObject();
  32. if(empty($this->userData)) {
  33. $_SESSION['logged'] = "no-logged";
  34. }
  35. }
  36. }
  37.  
  38. public function login($username, $password) {
  39. global $PDO;
  40. $select = $PDO->prepare('SELECT username, id FROM servers WHERE (username=:username AND password=:password) LIMIT 1');
  41. $select->bindValue('username', $username);
  42. $select->bindValue('password', $password);
  43. $select->execute();
  44. if($select->rowCount() < 1) {
  45.  
  46. $_SESSION['logged'] = "no-logged";
  47.  
  48. } else {
  49.  
  50. $data = $select->fetchObject();
  51. $uid = $data->id;
  52. $user = $data->username;
  53.  
  54. $_SESSION['uid'] = $uid;
  55. $_SESSION['user'] = $user;
  56. $_SESSION['logged'] = "logged";
  57.  
  58. header('Location: /index.php');
  59.  
  60. }
  61. }
  62.  
  63. public function dataUser($type) {
  64. global $PDO;
  65. if(!empty($this->userData->id) && !empty($this->userData->username)) {
  66. switch($type) {
  67. case 'id':
  68. return $this->userData->id;
  69. break;
  70. case 'user':
  71. return $this->userData->username;
  72. break;
  73. case 'firstname':
  74. return $this->userData->firstname;
  75. break;
  76. case 'lastname':
  77. return $this->userData->lastname;
  78. break;
  79. case 'email':
  80. return $this->userData->email;
  81. break;
  82. case 'pass':
  83. return $this->userData->password;
  84. break;
  85. case 'picture':
  86. return $this->userData->picture;
  87. break;
  88. }
  89. } else {
  90. return false;
  91. }
  92. }
  93.  
  94. }
  95.  
  96. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement