Weird session_write_close() issue

I’m getting a weird couple of errors atm:

Warning: session_write_close(): Session callback expects true/false return value in Unknown on line 0

Warning: session_write_close(): Failed to write session data (user). Please verify that the current setting of session.save_path is correct (C:/wamp/tmp) in Unknown on line 0

PDO has got emulated prepares turned off

Session handler object set up and called with:

$db_session_handler = new db_session_handler($db);
session_set_save_handler($db_session_handler,true);

session_start called just after that, the “session object” as it stands:

<?php

class db_session_handler implements SessionHandlerInterface {
    
    public function __construct($db) {
        $this->db = $db;
    }
    
    public function open($save_path,$session_name) {
        $this->db;
        return true;
    }
    
    public function close() {
        unset($this->db);
        return true;
    }
    
    public function read($session_id) {
        
        if (!isset($session_id)) {
            $session_id='';
        }
        
        try {
            $sql="
                SELECT
                    sess_data
                FROM
                    ue_user_session
                WHERE
                    sess_id = :sess_id
            ";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':sess_id', $session_id);
            $stmt->execute();
            $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
            if (count($res) <> 1 ) {
                return false;
            } else {
                return $res[0]['sess_data'];
            }
        }
        catch (PDOException $e) {
            error_log('Error reading the session data table in the session reading method.',3,"C:\wamp\www\universal_empires\test_log.txt");
            error_log(" Query with error: $sql",3,"C:\wamp\www\universal_empires\test_log.txt");
            error_log(" Reason given: $e->getMessage()",3,"C:\wamp\www\universal_empires\test_log.txt");
            return false;
        }
    }

    public function write($session_id,$session_data) {

        if (isset($_SESSION['user_id'])) {
            $user_id = (int) $_SESSION['user_id'];
        } else {
            $user_id= (int) 0;
        }

        try {
            $sql="
                SELECT
                    sess_data
                FROM
                    ue_user_session
                WHERE
                    sess_id = :sess_id
            ";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':sess_id', $session_id);
            $stmt->execute();
            $res = $stmt->fetchAll(PDO::FETCH_ASSOC);

        }
        catch (PDOException $e) {
            error_log('Error reading the session data table in the session reading method.',3,"C:\wamp\www\universal_empires\test_log.txt");
            error_log(" Query with error: $sql",3,"C:\wamp\www\universal_empires\test_log.txt");
            error_log(" Reason given: $e->getMessage()",3,"C:\wamp\www\universal_empires\test_log.txt");
            return false;
        }
        
        if (empty($res)) {
            try {
                if (count($res) === 0) {

                    $sql="
                        INSERT INTO
                            ue_user_session
                        (
                              sess_id
                            , user
                            , start
                            , last_activity
                            , expires
                            , sess_data
                        )
                        VALUES
                            (
                                  :sess_id
                                , 0
                                , NOW()
                                , NOW()
                                , DATE_ADD(NOW(), INTERVAL 30 MINUTE)
                                , :sess_data                        
                            )                
                    ";

                    $stmt = $this->db->prepare($sql);
                    $stmt->bindParam(':sess_id', $session_id);
                    $stmt->bindParam(':sess_data', $session_data);
                    $stmt->execute();
                }
            }
            catch (PDOException $e) {
                error_log('Error reading the session data table in the session reading method.',3,"C:\wamp\www\universal_empires\test_log.txt");
                error_log(" Query with error: $sql",3,"C:\wamp\www\universal_empires\test_log.txt");
                error_log(" Reason given: $e->getMessage()",3,"C:\wamp\www\universal_empires\test_log.txt");
                return false;
            }
        } else {
            
            try {
                $sql="
                    UPDATE
                        ue_user
                    SET
                        last_activity = NOW()
                    WHERE
                        id =  :user_id
                ";
                $stmt = $this->db->prepare($sql);
                $stmt->bindParam(':user_id', $user_id);
                $stmt->execute();
            }
            catch (PDOException $e) {
                error_log('Error reading the session data table in the session reading method.',3,"C:\wamp\www\universal_empires\test_log.txt");
                error_log(" Query with error: $sql",3,"C:\wamp\www\universal_empires\test_log.txt");
                error_log(" Reason given: $e->getMessage()",3,"C:\wamp\www\universal_empires\test_log.txt");
                return false;
            }
            try {
                $sql="
                    UPDATE
                        ue_user_session
                    SET
                          last_activity = NOW()
                        , expires = DATE_ADD(NOW(), INTERVAL 30 MINUTE)
                        , sess_data = :sess_data
                        , user = :user_id
                    WHERE
                        sess_id = :sess_id                
                ";
            
                $stmt = $this->db->prepare($sql);
                $stmt->bindParam(':sess_data', $session_data);
                $stmt->bindParam(':user_id', $user_id);
                $stmt->bindParam(':sess_id', $session_id);
                $stmt->execute();
                return true;
                }
            catch (PDOException $e) {
                error_log('Error reading the session data table in the session reading method.',3,"C:\wamp\www\universal_empires\test_log.txt");
                error_log(" Query with error: $sql",3,"C:\wamp\www\universal_empires\test_log.txt");
                error_log(" Reason given: $e->getMessage()",3,"C:\wamp\www\universal_empires\test_log.txt");
                return false;
            }
        }
    }
    
    public function destroy($session_id) {
        try {
            $sql="
                DELETE FROM
                    ue_user_session
                WHERE
                    sess_id = :sess_id
            ";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':sess_id', $session_id);
            $stmt->execute();
            return true;
        }
        catch (PDOException $e) {
            error_log('Error reading the session data table in the session reading method.',3,"C:\wamp\www\universal_empires\test_log.txt");
            error_log(" Query with error: $sql",3,"C:\wamp\www\universal_empires\test_log.txt");
            error_log(" Reason given: $e->getMessage()",3,"C:\wamp\www\universal_empires\test_log.txt");
            return false;
        }
    }

    public function gc($max_lifetime) {
        try {
            $sql="
                DELETE FROM
                    ue_user_session
                WHERE
                    last_activity < expires
            ";
            $stmt = $this->db->prepare($sql);
            $stmt->execute();
        }
        catch (PDOException $e) {
            error_log('Error reading the session data table in the session reading method.',3,"C:\wamp\www\universal_empires\test_log.txt");
            error_log(" Query with error: $sql",3,"C:\wamp\www\universal_empires\test_log.txt");
            error_log(" Reason given: $e->getMessage()",3,"C:\wamp\www\universal_empires\test_log.txt");
            return false;
        }
    }

    public function __destruct() {
        session_write_close(true);
    }
}
?>

Only upgraded to PHP 7 about an hour ago. It’s only happening when starting a new session

Problem solved, needed:

return true;

after inserting the session. Looks like PHP7 is stricter with what it allows with/for custom session handlers

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.