V
V
VaaL20042014-08-30 09:34:24
PHP
VaaL2004, 2014-08-30 09:34:24

Why doesn't it write the session to the database?

Hello, there is a php script, sessions are actively used in it, and it was necessary to store sessions in the database.
The following code was found:
Source on Github
And, in fact, inserted into the script as shown in the example:

require_once('sessions.php'); // Данный скрипт лежит в этом файле

$session = new MySessionHandler(); //Имя класса пришлось сменить, с какой-то версии php SessionHandler это зарезервированное имя

// add db data
$session->setDbDetails('localhost', 'root', '', 'session_db');

// OR alternatively send a MySQLi ressource
// $session->setDbConnection($mysqli);

 $session->setDbTable('session_db');

session_set_save_handler(array($session, 'open'),
                         array($session, 'close'),
                         array($session, 'read'),
                         array($session, 'write'),
                         array($session, 'destroy'),
                         array($session, 'gc'));

// The following prevents unexpected effects when using objects as save handlers.
register_shutdown_function('session_write_close');

session_start();



/*
Далее идёт код, в котором идёт работа с сессиями, на файлах всё происходило нормально
*/

In fact, what is the problem: it does not show any errors, it stopped creating files with sessions (fairly), but it does not write a line to the database! :(
Can anyone come across this? Tell me, please?
Version PHP 5.4.26

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Andreevich, 2014-08-30
@VaaL2004

Just checked - everything works. PHP 5.4.29.
After the line session_start(); I write "$_SESSION['test'] = 'hello';" and the session is written to the database.
Try to see what comes in the write method of the SessionHandler class:

public function write($id, $data) {
        echo $data; exit;

        $sql = sprintf("REPLACE INTO %s VALUES('%s', '%s', '%s')",
        ...
    }

I have this "test|s:5:"hello";".
The problem is somewhere with you, if you can’t figure it out, post more code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question