|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-11-27 23:05 UTC] jfoster at cervistech dot com
Description:
------------
When using session_set_save_handler to implement a custom database based session handler, PHP is throwing the following warning on session_start():
PHP Warning: session_start(): Failed to read session data: user (path: C:\PHP\session\)
The session handler is functioning correctly and reading/writing correctly to the DB, but is throwing the warning when no files are being used for sessions.
This same code has functioned without throwing the warning for every version of PHP since PHP 5.4. The warning only started occurring in 7.1
Test script:
---------------
$sess = new SessionManager();
session_start();
class SessionManager
{
var $life_time;
#function SessionManager()
function __construct()
{
// Read the maxlifetime setting from PHP
$this->life_time = get_cfg_var("session.gc_maxlifetime");
// Register this object as the session handler
session_set_save_handler(
array($this, "_open"),
array($this, "_close"),
array($this, "_read"),
array($this, "_write"),
array($this, "_destroy"),
array($this, "_gc")
);
}
function _open()
{
return true;
}
function _close()
{
return true;
}
function _read($id)
{
//code to read session data from db based on $id
}
function _write($id, $data)
{
//code to write/update session data to db based on $id
return TRUE;
}
function _destroy($id)
{
//code to delete session data from db based on $id
return TRUE;
}
function _gc()
{
//code to delete session data from db based on timestamp
return true;
}
}
Expected result:
----------------
Session handler to process session without throwing read file warning
Actual result:
--------------
Session handler processes session data from db successfully, but throws file read error.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 06:00:02 2025 UTC |
Do you get read() error always or on occasion? Could you check content of $data? public function read($id) { global $cid; // Set empty result $data = ''; // Fetch session data from the selected database $sql = $cid->prepare("SELECT session_data FROM sessions WHERE session_id = ?"); $sql->bind_param("s", $id); $sql->execute() or trigger_error("Session Error: " . mysqli_error($cid). " -- Query: " . $sql); $rs = $sql->get_result(); $a = mysqli_num_rows($rs); if($a > 0) { $row = mysqli_fetch_assoc($rs); $data = $row['session_data']; } #trigger_error("read - " . $_SERVER['REQUEST_URI']); if (!is_string($data)) var_dump($data); // <-- Something like this return (string)$data; }