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 705405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:02:59+00:00 2026-05-14T04:02:59+00:00

I’m experimenting with PHP’s session_set_save_handler and I’d like to use a PDO connection to

  • 0

I’m experimenting with PHP’s session_set_save_handler and I’d like to use a PDO connection to store session data.

I have this function as a callback for write actions:

function _write($id, $data) {
    logger('_WRITE ' . $id . ' ' . $data);
    try {
        $access = time();
        $sql = 'REPLACE INTO sessions SET id=:id, access=:access, data=:data';
        logger('This is the last line in this function that appears in the log.');
        $stmt = $GLOBALS['db']->prepare($sql);
        logger('This never gets logged! :(');
        $stmt->bindParam(':id', $id, PDO::PARAM_STR);
        $stmt->bindParam(':access', $access, PDO::PARAM_INT);
        $stmt->bindParam(':data', $data, PDO::PARAM_STR);
        $stmt->execute();
        $stmt->closeCursor();
        return true;
    } catch (PDOException $e) {
        logger('This is never executed.');
        logger($e->getTraceAsString());
    }
}

The first two log messages always show up, but the third one right after $stmt = $GLOBALS['db']->prepare($sql) never makes it to the log file and there’s no trace of an exception either.

The sessions db table remains empty.

The log message from the _close callback is always present.

Here’s how I connect to the database:

$db = new PDO('mysql:host=' . DBHOST . ';dbname=' . DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

I have PHP 5.2.10.

I tried to simply run $GLOBALS['db']->exec($sql) with a “manually prepared” $sql content, but it still failed silently. The query itself is all right I was able to execute it via the db console.


Edit:

After VolkerK has identified the problem I’ve found this article which explains the reason behind this weird phenomena. Perhaps it could be informative to others as well.


2nd edit:

The least painful, magic solution is that I had to add the below function call to the very end of my front controller (the main index.php) file:

session_write_close();
  • 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-05-14T04:03:00+00:00Added an answer on May 14, 2026 at 4:03 am

    My bets are on: $GLOBALS[‘db’] is not set or not an instance of pdo (anymore?) and therefor a PHP Fatal error: Call to a member function prepare() on a non-object occurs and php bails out.

    $sql = 'REPLACE INTO sessions SET id=:id, access=:access, data=:data';
    logger('This is the last line in this function that appears in the log.');
    if ( !isset($GLOBALS['db']) ) {
      logger('there is no globals[db]');
      return;
    }
    else if ( !is_object($GLOBALS['db']) ) {
      logger('globals[db] is not an object');
      return;
    }
    else if ( !($GLOBALS['db'] instanceof PDO) ) {
      logger('globals[db] is not a PDO object');
      return;
    }
    else {
      logger('globals[db] seems ok');
    }
    
    $stmt = $GLOBALS['db']->prepare($sql);
    logger('This never gets logged! :(');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I hope I'm not misunderstanding, but my advice is: create… May 14, 2026 at 6:06 pm
  • Editorial Team
    Editorial Team added an answer Authlogic provides API for dealing with sub domain based authentication.… May 14, 2026 at 6:06 pm
  • Editorial Team
    Editorial Team added an answer Here's a kickoff example, you can copy'n'paste'n'run it. <!doctype html>… May 14, 2026 at 6:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.