Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php $user = User::find_by_id($user_id); ?>
- <h1>Hello, <?php echo $user->username; ?></h1>
- <?php
- class User
- {
- protected static $db_table = "users";
- public $id;
- public $username;
- public $password;
- public $first_name;
- public $last_name;
- private function has_the_attribute($the_attribute)
- {
- $object_properties = get_object_vars($this);
- return array_key_exists($the_attribute, $object_properties);
- }
- public static function instantation($the_record)
- {
- $the_object = new self;
- foreach ($the_record as $the_attribute => $value) {
- if ($the_object->has_the_attribute($the_attribute)) {
- $the_object->$the_attribute = $value;
- }
- }
- return $the_object;
- }
- public static function find_this_query($sql)
- {
- global $database;
- $result_set = $database->query($sql);
- $the_object_array = [];
- while ($row = mysqli_fetch_array($result_set)) {
- $the_object_array[] = self::instantation($row);
- }
- return $the_object_array;
- }
- public static function find_all()
- {
- return self::find_this_query("SELECT * FROM " . static::$db_table . " ");
- }
- public static function find_by_id($user_id)
- {
- global $database;
- $the_result_array = self::find_this_query("SELECT * FROM " . self::$db_table . " WHERE id = $user_id");
- return !empty($the_result_array) ? array_shift($the_result_array) : false;
- }
- public static function verify_user($username, $password)
- {
- global $database;
- $username = $database->escape_string($username);
- $password = $database->escape_string($password);
- $sql = "SELECT * FROM " . self::$db_table . " WHERE ";
- $sql .= "username = '{$username}' ";
- $sql .= "AND password = '{$password}'";
- $the_result_array = self::find_this_query($sql);
- return !empty($the_result_array) ? array_shift($the_result_array) : false;
- }
- }
- $user = new User();
- <?php
- class Session
- {
- private $signed_in = false;
- public $user_id;
- public $message;
- public function __construct()
- {
- session_start();
- $this->check_the_login();
- $this->check_message();
- }
- public function login($user)
- {
- if ($user) {
- $this->user_id = $_SESSION['user_id'] = $user->id;
- $this->signed_in = true;
- }
- }
- public function logout()
- {
- unset($_SESSION['user_id']);
- unset($this->user_id);
- $this->signed_in = false;
- }
- private function check_the_login()
- {
- if (isset($_SESSION['user_id'])) {
- $this->user_id = $_SESSION['user_id'];
- $this->signed_in = true;
- } else {
- unset($this->user_id);
- $this->signed_in = false;
- }
- }
- public function is_signed_in()
- {
- return $this->signed_in;
- }
- public function message($msg="")
- {
- if (!empty($msg)) {
- $_SESSION['message'] = $msg;
- } else {
- return $this->message;
- }
- }
- public function check_message()
- {
- if (isset($_SESSION['message'])) {
- $this->message = $_SESSION['message'];
- unset($_SESSION['message']);
- } else {
- $this->message = "";
- }
- }
- }
- $session = new Session();
Add Comment
Please, Sign In to add comment