Answer the question
In order to leave comments, you need to log in
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();
/*
Далее идёт код, в котором идёт работа с сессиями, на файлах всё происходило нормально
*/
Answer the question
In order to leave comments, you need to log in
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')",
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question