2016-01-06 63 views
-1

我正在使用oop pdo創建登錄系統。當我登錄用戶時,會話沒有被創建。我認爲這與我的數據庫類有關。用戶的憑證在數據庫中是正確的,並且連接憑證是正確的。用戶會話在登錄後未被存儲

用戶等級:

class USER 
{ 

    public function login($uname,$upass) 
    { 
     try 
     { 
      $stmt = Database::getInstance()->getConnection()->prepare("SELECT * FROM users WHERE user_name=:uname LIMIT 1"); 
      $stmt->execute(array(':uname'=>$uname)); 
      $userRow=$stmt->fetch(PDO::FETCH_ASSOC); 
      if($stmt->rowCount() > 0) 
      { 
      if(password_verify($upass, $userRow['user_pass'])) 
      { 
      $_SESSION['user_session'] = $userRow['user_id']; 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
      } 
     } 
     catch(PDOException $e) 
     { 
      echo $e->getMessage(); 
     } 
    } 

數據庫類:

<?php 
class Database { 


    private $_connection; 
    private static $_instance; //The single instance 


    /* 
    Get an instance of the Database 
    @return Instance 
    */ 
    public static function getInstance() { 
     if(!self::$_instance) { // If no instance then make one 
      self::$_instance = new self(); 
     } 
     return self::$_instance; 
    } 
    // Constructor 
    private function __construct() { 


try 
{ 
    $dsn = 'mysql:host=hostnmae;dbname=database name'; // define host name and database name 
    $username = 'username'; // define the username 
    $pwd='password'; // password 
    $this->_connection = new PDO($dsn, $username, $pwd); 


} 
catch(PDOException $e) 
{ 
    echo $e->getMessage(); 
} 

    } 
    // Magic method clone is empty to prevent duplication of connection 
    private function __clone() { } 
    // Get mysqli connection 
    public function getConnection() { 
     return $this->_connection; 
    } 
} 
?> 
+0

哪裏是你的'session_start()'? – Daan

+0

其在我的init.php文件中被包含到我的header.php中,它在每個頁面的頂部 – fred9999999999

+0

我在我的頭文件中使用obj_start()可能這是原因 – fred9999999999

回答

0

是的,你需要在session_start()在你的init.php 和ob_start需要在session_start之前調用

+0

感謝您的幫助它現在工作 – fred9999999999