我正在使用mongoDB和PHP为我的网页创建一个登录部分。目前我可以验证我的用户凭据,检查它是否真实。我的问题是,当我登录时,它不会在php中保持会话。我从一本书中获取了一些代码,其中展示了如何将会话存储在我的mongoDB数据库中。我很确定它必须是有错误的会话,因为它不会将信息带到下一页。
我的会话代码
<?php
require_once('dbconnection.php');
class SessionManager
{
//name of collection where sessions will be stored
const COLLECTION = 'sessions';
//Expire session after 10 mins in inactivity
const SESSION_TIMEOUT = 600;
//Expire session after 1 hour
const SESSION_LIFESPAN = 3600;
//name of the session cookie
const SESSION_NAME = 'mongosessid';
const SESSION_COOKIE_PATH = '/';
const SESSION_COOKIE_DOMAIN = '';
private $_mongo;
private $_collection;
private $_currentSession;
public function __construct()
{
$this->_mongo = DBConnection::instantiate();
$this->_collection = $this->_mongo->
getCollection(SessionManager::COLLECTION);
session_set_save_handler(
array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc')
);
//Set session garbage collection period
ini_set('session.gc_maxlifetime',
SessionManager::SESSION_LIFESPAN);
//set session cookie configurations
session_set_cookie_params(
SessionManager::SESSION_LIFESPAN,
SessionManager::SESSION_COOKIE_PATH,
SessionManager::SESSION_COOKIE_DOMAIN
);
//Replace 'PHPSESSID' with 'mongosessid' as the
//session name
session_name(SessionManager::SESSION_NAME);
session_cache_limiter('nocache');
//start the session
session_start();
}
public function open($path, $name)
{
return true;
}
public function close()
{
return true;
}
public function read($sessionId)
{
$query = array(
'session_id' => $sessionId,
'timedout_at' => array('$gte' => time()),
'expired_at' => array('$gte' => time() -
SessionManager::SESSION_LIFESPAN)
);
$result = $this->_collection->findOne($query);
$this->_currentSession = $result;
if(!isset($result['data']))
{
return '';
}
return $result['data'];
}
public function write($sessionId, $data)
{
$expired_at = time() + self::SESSION_TIMEOUT;
$new_obj = array(
'data' => $data,
'timedout_at' =>
time() + self::SESSION_TIMEOUT,
'expired_at' =>
(empty($this->_currentSession)) ?
time()+ SessionManager::SESSION_LIFESPAN
: $this->_currentSession['expired_at']
);
$query = array('session_id' => $sessionId);
$this->_collection->update(
$query,
array('$set' => $new_obj),
array('upsert' => True)
);
return True;
}
public function destroy($sessionId)
{
$this->_collection->remove(array('session_id' =>
$sessionId));
return True;
}
public function gc()
{
$query = array( 'expired_at' => array('$lt' => time()));
$this->_collection->remove($query);
return True;
}
public function __destruct()
{
session_write_close();
}
}
//initiate the session
$session = new SessionManager
();
user.php的
<?php
require_once('dbconnection.php');
require_once('session.php');
class User
{
const COLLECTION = 'users';
private $_mongo;
private $_collection;
private $_user;
public function __construct()
{
$this->_mongo = DBConnection::instantiate();
$this->_collection = $this->_mongo->
getCollection(User::COLLECTION);
if ($this->isLoggedIn()) $this->_loadData();
}
public function isLoggedIn()
{
return isset($_SESSION['user_id']);
}
public function authenticate($username, $password)
{
$query = array(
'username' => $username,
'password' => $password
);
$this->_user = $this->_collection->findOne($query);
if (empty($this->_user)) return False;
$_SESSION['user_id'] = (string) $this->_user['_id'];
return True;
}
public function logout()
{
unset($_SESSION['user_id']);
}
public function __get($attr)
{
if (empty($this->_user))
return Null;
switch($attr)
{
case 'address':
$address = $this->_user['address'];
return sprintf('country: %s, city: %s, street: %', $address['country'],
$address['city'], $address['street']);
case 'city':
return $this->_user['address']['city'];
case 'country':
return $this->_user['address']['country'];
case 'password':
return NULL;
default:
return (isset($this->_user[$attr])) ?
$this->_user[$attr] : NULL;
}
}
private function _loadData()
{
$id = new MongoId($_SESSION['user_id']);
$this->_user = $this->_collection->findOne(array('_id'
=> $id));
}
}
我的两个php / HTML页面
<?php
$action = (!empty($_POST['login']) &&
($_POST['login'] === 'Log in')) ? 'login'
: 'show_form';
switch($action)
{
case 'login':
require('session.php');
require('user.php');
$user = new User();
$username = $_POST['username'];
$password = $_POST['password'];
if ($user->authenticate($username, $password))
{
header('location: startpage.php');
exit;
}
else
{
$errorMessage = "Username/password did not match.";
break;
}
case 'show_form':
default:
$errorMessage = NULL;
}
?>
Seconcd php / HTML页面(不含HTML)
<?php
require('session.php');
require('user.php');
$user = new User();
if (!$user->isLoggedIn())
{
header('location: index.php');
exit;
}
else {
header('location: startpage.php');
break;
}
?>
感谢任何帮助,感谢所有读过这些代码的人。