Sorry, wrong stackoverflow link. Here is the correct one:
http://stackoverflow.com/questions/10150296/cant-make-custom-session-save-handler-workno-registered-method-called-in-cent#comment13018050_10150327
And here is the code for session:
<?php
class session
{
public static function init()
{
session_set_save_handler('session::open', 'session::close',
'session::read', 'session::write', 'session::destroy', 'session::gc');
}
public static function open($save_path, $session_name)
{
if (!is_dir($save_path)) {
mkdir($save_path, 0777);
}
return true;
}
public static function close()
{
return true;
}
public static function read($sid)
{
global $db, $user;
register_shutdown_function('session_write_close');
if (!isset($_COOKIE[session_name()])) {
$user = anonymousUser($sid);
return '';
}
$result = $db->query('SELECT s.data as session_data, s.* , u.* FROM
users u INNER JOIN sessions s ON u.uid = s.uid WHERE s.sid = "' .
$db->escape($sid) .
'" AND timestamp >= ' . $db->escape(TIMESTAMP -
Bl_Config::get('session.lifetime', 10800)));
$user = $result->row();
if ($user) {
$data = $user->session_data;
unset($user->passwd, $user->session_data);
if ($user->uid > 0 && $user->status == 1) {
$userInstance = User_Model::getInstance();
$user->roles = $userInstance->getUserRoles($user->uid);
$user->roles[] = User_Model::ROLE_AUTHENTICATED_USER;
$user->permissions = array();
$user->data = (isset($user->data) && $user->data) ?
unserialize($user->data) : array();
foreach ($user->roles as $rid) {
$user->permissions = array_merge($user->permissions,
$userInstance->getRolePermissions($rid));
}
$user->permissions = array_unique($user->permissions);
} else {
$user = anonymousUser($sid);
}
return $data;
} else {
$user = anonymousUser($sid);
return '';
}
}
public static function write($sid, $data)
{
global $db, $user;
if (!isset($user) || ($user->uid == 0 &&
empty($_COOKIE[session_name()]) && empty($data))) {
return true;
}
$uri = '/' . Bl_Core::getUri();
$db->exec('UPDATE sessions SET uid = ' . $db->escape($user->uid) .
', ip = "' . $db->escape(ipAddress()) .
'", uri = "' . $db->escape($uri) . '", data = "' .
$db->escape($data) . '", timestamp = ' .
$db->escape(TIMESTAMP) . ' WHERE sid = "' . $db->escape($sid) . '"');
if (!$db->affected()) {
$db->exec('INSERT IGNORE INTO sessions (sid, uid, ip, uri, data,
timestamp) VALUES ("' . $db->escape($sid) .
'", ' . $db->escape($user->uid) . ', "' .
$db->escape(ipAddress()) . '", "' . $db->escape($uri) . '", "' .
$db->escape($data) . '", ' . $db->escape(TIMESTAMP) . ')');
}
return true;
}
public static function destroy($sid)
{
global $db;
$db->exec('DELETE FROM sessions WHERE sid = "' . $db->escape($sid)
. '"');
return true;
}
public static function gc($lifetime)
{
global $db;
$db->exec('DELETE FROM sessions WHERE timestamp < ' .
$db->escape(TIMESTAMP - Bl_Config::get('session.lifetime', 10800)));
return true;
}
public static function count($timestamp = 0, $hasAnonymous = true)
{
global $db;
if (!$hasAnonymous) {
$cond = ' AND uid > 0';
} else {
$cond = '';
}
$result = $db->query('SELECT COUNT(0) FROM sessions WHERE timestamp
> ' . $timestamp . $cond);
return $result->one();
}
}
On 2012/4/15 14:53, Mingda wrote:
Hi, All,
I can't see the post I sent several hours ago, if repeated, please reply
to this one. Thanks!
System: CentOS 5.5; PHP version is 5.1.6.
I met a strange problem associate with session_save_handler in current
environment(The same code can work well in my local windows platform and
ubuntu system).
I just want to use a customized session save handler to be triggered, so
that I can call my own logic to handling the session. The testing in
local is pretty great but when migration to the VPS, it bring me the
following error:
Fatal error: session_start() [<a
href='function.session-start'>function.session-start</a>]: Failed to
initialize storage module: user (path: /tmp)
The default value for session save handler and session save path in
php.ini are:
session.save_handler = files
session.save_path = "/tmp"
session.name = PHPSESSID
And the bottom are the code for the session handler. I first called
ob_start(), and after calling session::init(), I called session_start().
Then the fatal error happen. It did not trigger any function in the
"session" class.
I tried change the php.ini from session.save_handler = user, but the
error remains. And I found no matter what session.save_handler type is,
after calling session_set_save_handler(), the session.save_handler will
always automatically changed to 'user', that's why the Fatal error info
shows user (path: /tmp).
Can anybody help me out for such error? I was stuck by this issue for
more than 2 days, but still haven't get any clue!
You can view more details from stackoverflow if you want. Here is the link:
http://stackoverflow.com/questions/8845924/session-set-save-handler-class-for-database-not-working
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php