用户会话在登录后未存储

时间:2016-01-06 15:11:27

标签: php session authentication pdo

我正在使用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;
    }
}
?>

1 个答案:

答案 0 :(得分:0)

是的,你需要在init.php中使用session_start() 并且需要在session_start

之前调用ob_start
相关问题