Guest User

Untitled

a guest
Nov 29th, 2018
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. <?php $user = User::find_by_id($user_id); ?>
  2. <h1>Hello, <?php echo $user->username; ?></h1>
  3.  
  4. <?php
  5.  
  6. class User
  7. {
  8. protected static $db_table = "users";
  9. public $id;
  10. public $username;
  11. public $password;
  12. public $first_name;
  13. public $last_name;
  14.  
  15. private function has_the_attribute($the_attribute)
  16. {
  17. $object_properties = get_object_vars($this);
  18. return array_key_exists($the_attribute, $object_properties);
  19. }
  20.  
  21. public static function instantation($the_record)
  22. {
  23. $the_object = new self;
  24. foreach ($the_record as $the_attribute => $value) {
  25. if ($the_object->has_the_attribute($the_attribute)) {
  26. $the_object->$the_attribute = $value;
  27. }
  28. }
  29. return $the_object;
  30. }
  31.  
  32. public static function find_this_query($sql)
  33. {
  34. global $database;
  35. $result_set = $database->query($sql);
  36. $the_object_array = [];
  37. while ($row = mysqli_fetch_array($result_set)) {
  38. $the_object_array[] = self::instantation($row);
  39. }
  40. return $the_object_array;
  41. }
  42.  
  43. public static function find_all()
  44. {
  45. return self::find_this_query("SELECT * FROM " . static::$db_table . " ");
  46. }
  47.  
  48. public static function find_by_id($user_id)
  49. {
  50. global $database;
  51. $the_result_array = self::find_this_query("SELECT * FROM " . self::$db_table . " WHERE id = $user_id");
  52. return !empty($the_result_array) ? array_shift($the_result_array) : false;
  53. }
  54.  
  55. public static function verify_user($username, $password)
  56. {
  57. global $database;
  58. $username = $database->escape_string($username);
  59. $password = $database->escape_string($password);
  60.  
  61. $sql = "SELECT * FROM " . self::$db_table . " WHERE ";
  62. $sql .= "username = '{$username}' ";
  63. $sql .= "AND password = '{$password}'";
  64.  
  65. $the_result_array = self::find_this_query($sql);
  66. return !empty($the_result_array) ? array_shift($the_result_array) : false;
  67. }
  68. }
  69.  
  70. $user = new User();
  71.  
  72. <?php
  73.  
  74. class Session
  75. {
  76. private $signed_in = false;
  77. public $user_id;
  78. public $message;
  79.  
  80. public function __construct()
  81. {
  82. session_start();
  83. $this->check_the_login();
  84. $this->check_message();
  85. }
  86.  
  87. public function login($user)
  88. {
  89. if ($user) {
  90. $this->user_id = $_SESSION['user_id'] = $user->id;
  91. $this->signed_in = true;
  92. }
  93. }
  94.  
  95. public function logout()
  96. {
  97. unset($_SESSION['user_id']);
  98. unset($this->user_id);
  99. $this->signed_in = false;
  100. }
  101.  
  102. private function check_the_login()
  103. {
  104. if (isset($_SESSION['user_id'])) {
  105. $this->user_id = $_SESSION['user_id'];
  106. $this->signed_in = true;
  107. } else {
  108. unset($this->user_id);
  109. $this->signed_in = false;
  110. }
  111. }
  112.  
  113. public function is_signed_in()
  114. {
  115. return $this->signed_in;
  116. }
  117.  
  118. public function message($msg="")
  119. {
  120. if (!empty($msg)) {
  121. $_SESSION['message'] = $msg;
  122. } else {
  123. return $this->message;
  124. }
  125. }
  126.  
  127. public function check_message()
  128. {
  129. if (isset($_SESSION['message'])) {
  130. $this->message = $_SESSION['message'];
  131. unset($_SESSION['message']);
  132. } else {
  133. $this->message = "";
  134. }
  135. }
  136. }
  137.  
  138. $session = new Session();
Add Comment
Please, Sign In to add comment