Session set save handler problem
Posted: Wed Sep 10, 2008 4:20 pm
I'm using a user custom session through DB. And I use MVC (built myself) with index.php controlling the routing
controller.php
index.php
And my session file looks like this
.
I'm able to get everything working with my PHP native sessions,and even with my session handler Im able retrieve $_SESSION['role'] in index.php and the problem seems to be in redirecting the page, on doing session_start() and retrieving in user module and home.php, I get $_SESSION blank array, Can somebody throw some light on this
controller.php
Code: Select all
ini_set('session.save_handler', 'user');
if (version_compare(phpversion(), '5.0.0', '>=')) {
register_shutdown_function('session_write_close');
}
$OPT_SESSION = new Session();
session_set_save_handler(
array(&$OPT_SESSION,"open"),
array(&$OPT_SESSION,"close"),
array(&$OPT_SESSION,"read"),
array(&$OPT_SESSION,"write"),
array(&$OPT_SESSION,"destroy"),
array(&$OPT_SESSION,"gc")
);
Code: Select all
require_once('controller.php');
clearstatcache();
//$role = 1;
session_start();
if( isset($_GET['app']) ){
$x = $_GET['x']? $_GET['y']:'';
if(!isset($_GET['x']) || empty($_GET['y'])){
$y = 'index';
}else{
$action = $_GET['y'];
}
}
$user_id = 0;
if(isset($_POST['login'])){
$username = isset($_POST['username'])? $_POST['username']:'';
$password = isset($_POST['password'])? $_POST['password']:'';
if( !empty($username) && !empty($password)){
$user = new User($username,$password);
$authenticated = $user->isAuthenticated();
if(!$authenticated){
$error = true;
}
}else{
$error = true;
}
}
if($authenticated == true){
$_SESSION['userid'] = $user->user_id;
$_SESSION['role'] = $user->role_id;
}
if($_SESSION['role']> AUTHENTICATED_ROLE){
$isparam = (isset($_POST['mod']))? $_POST['mod'] : '';
if(!empty($isparam)){
header("Location: index.php?x=user&y=home");
exit;
}
}
//$accessObj = new CoreAcl();
if(!empty($x)){
include_once("modules"."/$x"."/$y".".php");
$default_content = false;
}else{
$default_content = true;
}
//$acl = new CoreAcl(2,2);
//$val = $acl->isValid(1.02);
if($default_content) {
require_once(CONTROLLER_DIR."/styles/template/header.php");
require_once(CONTROLLER_DIR."/styles/template/left.php");
$xtpl = new Xtemplate('styles/index.xtpl');
$xtpl->parse('main');
$xtpl->out('main');
require_once(CONTROLLER_DIR."/styles/template/right.php");
require_once(CONTROLLER_DIR."/styles/template/footer.php");
}
Code: Select all
Class Session{
public $lifetime;
protected $db;
public $data = array();
function __construct(){
$this->db = new DB();
if(!$this->db){
return false;
}
}
public function open($savePath,$sessionName){
$this->lifetime = get_cfg_var("session.gc_maxlifetime");
//$this->read(session_id()));
return true;
}
public function close(){
//echo $sessionID;
return true;
}
public function read($sessionID){
$sql = "SELECT session_data from sessions where session_key ='$sessionID'";
$res = $this->db->executeSelectArray($sql);
if(count($res) != 0){
$session_data = $res[0]['session_data'];
$session = unpackSessionData($session_data);
return $session;
}
return null;
}
public function write($sessionID,$sessionData){
$newExp = time() + $this->lifetime;
$sql = "SELECT session_data from sessions where session_key ='$sessionID' ";
$res = $this->db->executeSelectArray($sql);
if(count($res)>0){
$add = $res[0]['session_data'];
$update_query = "UPDATE sessions SET modify_date=NOW(),session_expires='".$newExp."',session_data='".$sessionData."' WHERE session_key='".$sessionID."' ";
$count = $this->db->executeQuery($update_query);
if($count >0 ){
return $this->read($sessionID);
}
}else{
$insert = "INSERT into sessions(session_key,session_data,begin_time,session_expires,create_date,modify_date) VALUES('$sessionID','".$sessionData."',NOW(),'$this->lifetime',NOW(),NOW())";
$count = $this->db->executeQuery($insert);
if($count >0 ){
return $this->read($sessionID);
}
return false;
}
}
public function destroy($sessionID){
$query = "DELETE FROM sessions where session_key='$sessionID'";
$rows = $this->db->executeQuery($query);
if($rows >0){
//$this->data = array();
return true;
}
return false;
}
public function gc($sessionMaxLifeTime){
$query = "DELETE FROM sessions where session_expires< '".time()."'";
$rows = $this->db->executeQuery($query);
return $rows;
}
}
I'm able to get everything working with my PHP native sessions,and even with my session handler Im able retrieve $_SESSION['role'] in index.php and the problem seems to be in redirecting the page, on doing session_start() and retrieving in user module and home.php, I get $_SESSION blank array, Can somebody throw some light on this