登录后如何动态显示数据库中的用户名?

时间:2018-11-29 15:07:42

标签: php oop session login

我需要在OOP PHP项目中动态显示登录用户的用户名。当我从数据库中键入正确的ID时,我可以显示它,但是当我尝试在函数find_by_id中定义属性$ user_id时,它显示错误。我需要有关如何定义$ user_id变量的帮助。这是我的代码:

index.php

<?php $user = User::find_by_id($user_id); ?>
<h1>Hello, <?php echo $user->username; ?></h1>

user.php

<?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();

session.php

<?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();

1 个答案:

答案 0 :(得分:0)

为了将其标记为接受,您实际上需要传递的用户ID而不是未初始化的变量,如果您的实例将其存储在会话中,那么我想它是: / p>

<?php $user = User::find_by_id($_SESSION['user_id']); ?>

注意:要使模板更简洁,可以对echo使用简写语法:

<h1>Hello, <?= $user->username; ?></h1>

要注意的另一件事是,您已经构建了一个Session类,但是由于某种原因,您仍然出于某种原因通过$_SESSION访问数据,因此需要进行一些设置/获取。最后,会话是您将要使用的很多东西,因此值得将该类静态化。

阅读材料

echo