2016-01-06 62 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