Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8047715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:11:12+00:00 2026-06-05T06:11:12+00:00

I am using a PHP session manager class that will handle user sessions in

  • 0

I am using a PHP session manager class that will handle user sessions in MongoDB, but it’s not working, and I can not for the life of me figure out why. Session variables set successfully, but will not persist into other pages. Connectivity with MongoDB is not the issue. I’ve determined the issue has to do with the session_set_save_handler, because if I comment that function out, native PHP session handling works fine.

Thanks in advance for your help.

...
require('database.class.php');

class SessionManager {

const COLLECTION = 'sessions';
const SESSION_TIMEOUT = 600;
const SESSION_LIFESPAN = 3600;
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);

    register_shutdown_function('session_write_close'); 

    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 config
    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');

    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() + SessionManager::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)
                                );
}

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;
}

}

$session = new SessionManager();

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:11:14+00:00Added an answer on June 5, 2026 at 6:11 am

    write is called after object destruction. Your $_mongo object has already been destroyed by the time you come to use it.

    Use session_write_close() with register_shutdown_function() to get around this.

    register_shutdown_function('session_write_close');
    

    In addition to this (because this was almost certainly happening), the options for the update query do not begin with a $ (a mistake I’ve made myself numerous times), you need upsert not $upsert

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using old php script that depends on session_register to gives warning messages but
I'm building a password manager in PHP where the user can store and access
I'm using CodeIgniter's Session class to manage my sessions for a cart/checkout system. The
I'm developing on GAE using Resin, it seems that my PHP session on the
I have a set of PHP server side scripts that maintain user session state
I've got a really simple login script using PHP's sessions to limit access, but
I am using PHP to settle the session timeout...I found a few solutions so
I am trying to use PHP session without using cookies. I have enabled session.use_trans_sid
I want to share my session variables in PHP using many subdomains. I have
i was developing my php application and using mysql to store session data and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.