Welcome! Log In Create A New Profile

Advanced

[PHP] Customized Session Handler can not work for PHP 5.1.6 and CentOS 5.5

Posted by Mingda 
Hi, All,

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
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();
}
}





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
On Sat, Apr 14, 2012 at 9:27 AM, Mingda <[email protected]> wrote:
> Hi, All,
>
> 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).
>

This is your clue on how to fix. What version of PHP are on Windows
and Ubuntu? If different, perhaps upgrade your CentOS' PHP? If the
same exact version on all 3 OSes, then consult CentOS :).

HTH,
Tommy

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
On Sun, Apr 15, 2012 at 12:27:00AM +0800, Mingda wrote:
> Hi, All,
>
> 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

Try changing the path to /var/lib/php/session

Are you being caught by selinux - try
setenforce off
and see if that makes it work.

--
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256 http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Hi, Thanks, it's originally is /var/lib/php/session, I double it's
privilege problem, so changed to /tmp.

And I Followed your advice for setenforce off, but can't make it work.

Mingda

On 2012/4/16 14:13, Alain Williams wrote:
> On Sun, Apr 15, 2012 at 12:27:00AM +0800, Mingda wrote:
>> Hi, All,
>>
>> 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
>
> Try changing the path to /var/lib/php/session
>
> Are you being caught by selinux - try
> setenforce off
> and see if that makes it work.
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, wrong link provided, correct link is:

http://stackoverflow.com/questions/10150296/cant-make-custom-session-save-handler-workno-registered-method-called-in-cent#comment13018050_10150327





On 2012/4/15 0:27, Mingda wrote:
> Hi, All,
>
> 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
> 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();
> }
> }
>
>
>
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Hi, Tommy,

Thank you!

You are great that after all other methods tried, I finally upgrade my
php version, and found it's worked!!!

The CentOS VPS use Cent OS 5.5 and PHP 5.1.6 as default, but it has
problem for supporting custom session save handlers. It's weird that no
one and no doc mentioned this!!!

My suggestions is using PHP Version 5.2.6 or later to using such feature.

Mingda

On 2012/4/16 10:45, Tommy Pham wrote:
> On Sat, Apr 14, 2012 at 9:27 AM, Mingda<[email protected]> wrote:
>> Hi, All,
>>
>> 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).
>>
>
> This is your clue on how to fix. What version of PHP are on Windows
> and Ubuntu? If different, perhaps upgrade your CentOS' PHP? If the
> same exact version on all 3 OSes, then consult CentOS :).
>
> HTH,
> Tommy


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login