2013-10-19 43 views
0

对于一个小个人项目,我需要将一个类实例附加到PHP会话中。 为此,我编写了下面的代码,但它不能像我期望的那样工作。例如,当我调用方法login并刷新页面时,我的课程中没有任何修改。我的意思是,就好像它每次都重置一遍。PHP会话 - 附加一个类实例

我的问题是:如何使用PHP会话在我的页面之间共享Core实例?

<?php 

class Core 
{ 
    /* Database */ 
    private $db = null; 

    /* User activity */ 
    private $loggedin = false; 
    private $userid = 0; 
    private $username = null; 
    private $password = null; 
    private $lastActivity = null; 

    /* Constructor */ 
    public function __construct() 
    { 
     $this->connect(); 
    } 

    /* Serialize */ 
    public function __sleep() 
    { 
     return array('loggedin', 'userid', 'username', 'password', 'lastActivity'); 
    } 

    /* Unserialize */ 
    public function __wakeup() 
    { 
     $this->connect(); 
    } 

    /* Connect to database */ 
    private function connect() 
    { 
     global $DB_HOSTNAME, $DB_BASENAME, $DB_USERNAME, $DB_PASSWORD; 

     try 
     { 
      $this->db = $db = new PDO('mysql:host='.$DB_HOSTNAME.';dbname='.$DB_BASENAME.';charset=utf8', $DB_USERNAME, $DB_PASSWORD); 
      $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
      $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     } 
     catch (Exception $e) 
     { 
      die('Erreur : ' . $e->getMessage()); 
     } 
    } 

    /* Get database */ 
    public function getDb() 
    { 
     return $this->db; 
    } 

    /* Create a new login session */ 
    public function login($userid, $username, $password) 
    { 
     $this->loggedin = true; 
     $this->userid = intval($userid); 
     $this->username = $username; 
     $this->password = $password; 
     $this->lastActivity = time(); 
    } 

    /* Close the login session */ 
    public function logout() 
    { 
     $this->loggedin = false; 
     $this->userid = 0; 
     $this->username = null; 
     $this->password = null; 
    } 

    /* Check if the session is running or not */ 
    public function isConnected() 
    { 
     if ($this->loggedin) 
     { 
      if ($this->lastActivity + 3600 < time()) 
      { 
       $this->logout(); 
       return false; 
      } 
      elseif (isset($this->password)) 
      { 
       $db = $this->db; 
       $query = $db->prepare('SELECT id FROM ptc_users WHERE id=:userid AND password=:password LIMIT 1;'); 
       $query->bindParam(':userid', $this->userid, PDO::PARAM_INT); 
       $query->bindParam(':password', $this->password, PDO::PARAM_STR); 
       $query->execute(); 

       if ($query->rowCount() == 1) 
       { 
        $this->lastActivity = time(); 
        return true; 
       } 
       else 
       { 
        return false; 
       } 
      } 
      else 
      { 
       return false; 
      } 
     } 

     return false; 
    } 

    /* Return the current user id */ 
    public function getUid() 
    { 
     return $this->userid; 
    } 

    /* Return the current username */ 
    public function getUsername() 
    { 
     return $this->username; 
    } 

    /* Return the hash signature of the password */ 
    public function hash($input) 
    { 
     return sha1('minad8rBxu' .$input. 'MigdVKXUCf'); 
    } 

    /* Return the user's ip */ 
    public function getIp() 
    { 
     if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) return $_SERVER['HTTP_X_FORWARDED_FOR']; 
     else return $_SERVER['REMOTE_ADDR']; 
    } 
} 

// Create a new core session if not started before 
if (!isset($_SESSION)) session_start(); 
if (!isset($_SESSION['core'])) 
{ 
    $core = new Core(); 
    $_SESSION['core'] = serialize($core); 
} 
else 
{ 
    $core = unserialize($_SESSION['core']); 
} 

?> 

我用魔术方法__sleep__wakeup避免序列化PDO实例。

感谢您的帮助。

回答

1

实例化或自动加载Core类,以便在整个应用程序中使用它。无需session..Or只是类名保存到会话..

用户名,时间戳,用户名和密码就保存到会话..

$_SESSION['instance'] = json_encode(array('username', 'timestamp', 'userid','password')); 

$object = json_decode($_SESSION['instance']); 

echo var_dump($object); 
+0

我不明白你的意思。你能解释一下吗? – Manitoba

+0

老兄,我从来没有听说过任何人将实例保存到会话中。他们所做的只是使用spl_autoload函数 –

+0

那么,我需要在每个页面上传递用户名,时间戳,用户ID和密码以检查用户是否连接。这就是为什么我需要将实例存储到会话中。这不是我第一次看到这个。如果你谷歌,许多人都这样做。 – Manitoba