Php 用户登录后未存储用户会话
我正在使用oop pdo创建一个登录系统。当我登录用户时,不会创建会话。我认为这与我的数据库类有关。数据库中的用户凭据正确,连接凭据正确 用户类别:Php 用户登录后未存储用户会话,php,session,authentication,pdo,Php,Session,Authentication,Pdo,我正在使用oop pdo创建一个登录系统。当我登录用户时,不会创建会话。我认为这与我的数据库类有关。数据库中的用户凭据正确,连接凭据正确 用户类别: class USER { public function login($uname,$upass) { try { $stmt = Database::getInstance()->getConnection()->prepare("SELECT * FROM users
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;
}
}
?>
是的,您需要init.php中的session_start()
在会话开始之前需要调用ob_start您的
会话开始()在哪里
?它在我的init.php文件中,包含在我的header.php文件中,该文件位于每个页面的顶部。我在头文件中使用obj_start(),这可能是原因吗