I created a class that will handle the session_set_save_handler but after executing session_start i will get server error this is my code,

<?php
defined('protect')||(header("HTTP/1.0 404 Not Found"));
require_once INCLUDES . '/query.php';
class MySQL_Session{
    private $dbConnect = null;
    protected $savePath;
    protected $sessionName;
    private $sessionTable;
    private $sessionColumn;

    public function __construct() {
        $c = new Configuration();
        $this->sessionTable = $c->sessionTable;
        $this->sessionColumn = $c->sessionColumn;
        session_set_save_handler(
             array($this, "mysql_session_open"),
             array($this, "mysql_session_close"),
             array($this, "mysql_session_select"),
             array($this, "mysql_session_write"),
             array($this, "mysql_session_destroy"),
             array($this, "mysql_session_garbage_collect")
         );
        register_shutdown_function('session_write_close');
    }

    public function mysql_session_open($session_path, $session_name) {
        $this->dbConnect = new DatabaseConnection();
        $this->savePath=$session_path;
        $this->sessionName=$session_name;
        return TRUE;
    }

    public function mysql_session_close() {
    return TRUE;
    } 

    public function mysql_session_select($SID) {
        $insert = new DatabaseQuery();

        $query = "SELECT value FROM sessioninfo
        WHERE SID = '$SID' AND
        expiration > ". time();

        $insert->select($query);

        if (num_rows()) {
            $row=mysql_fetch_assoc($insert->result);
            $value = $row['value'];
            return $value;
        } else {
            return "";
        }
    } 

    public function mysql_session_write($SID, $value) {
        $lifetime = get_cfg_var("session.gc_maxlifetime");
        $expiration = time() + $lifetime;

        $query = new DatabaseQuery();
        $result = $query->insert($this->sessionTable, implode(',',(array)@$this->sessionColumn), implode(',', array($SID,$expiration,$value)));

        if (!$result) {
            $field = array($this->sessionColumn['i'], $this->sessionColumn['e']);
            $value = array($SID, time());
            $operator = array('=','>');
            $query->where = string::MySQL_Where_Clause((array)$field,(array)$value,(array)$operator,array('OR'));
            $query->update($this->sessionTable, implode(',', array($this->sessionColumn['e'], $this->sessionColumn['v'])), implode(',', array($expiration,$value)));
        }
    } 

    public function mysql_session_destroy($SID) {
        $delete = new DatabaseQuery();
        $delete->where = string::MySQL_Update_Setter($this->sessionColumn['i'],$SID);
        $delete->delete($this->sessionTable);
    }

    public function mysql_session_garbage_collect($lifetime) {
        $delete = new DatabaseQuery();
        $delete->where = string::MySQL_Update_Setter($this->sessionColumn['e'], time() - $lifetime,'<');
        return $delete->delete($this->sessionTable);
    }
}

?>

then i will try to execute like this

<?php
defined('protect')||(header("HTTP/1.0 404 Not Found"));
class template{
    function render(){

        include INCLUDES.'/session.php';
        $session = new MySQL_Session();
         session_start();
        $_SESSION['adminToken']='test';
    }
}
?>

but i will get an error on session_start() but if i will try to remove the session_start() the code will run smoothly please help me about this

i will appreciate for any help

Actually PHP is great in that field. You have many things in your PHP script that I don’t get, it aint a class but a script … why ? … How are you going to call that ? Don’t you have a spl_autoload_register there? So what is that INCLUDE constant? And of course without knowing what this file is and what script is I couldn’t say much except that this is not a class and a class php file.

I suppose that you try to get the session out of a db … is that true ? If you are then I could share a way with you even in this scripting way.

Thank you for the reply. i already found the error it is on other class that will manipulate strings.

actually i am new in OOP and i am trying to implement an MVC framework.

BTW you mention about "spl_autoload_register" that seems new to me, could you give a link of a good tutorial about that. I will appreciate if you will

thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.