1

我遇到了一个奇怪的问题。linux中的会话保存处理程序无法正常工作(相同的代码在我本地的windows平台上可以正常工作。)。这是我的会话保存处理程序代码:

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

这是我调用会话保存处理程序的方式:

session::init();
session_start();

但是在静态类会话中没有调用任何方法。似乎应用程序在 session_start() 调用后停止继续运行。

我使用的php版本是PHP Version 5.1.6.我不知道是与php.ini有关还是db的某些权限导致了这样的问题?我正在使用 root db 用户并且已经设置了连接......

这是会话的 php.ini 设置(根据 Mike Purcell 的建议,我已将 session.save_path 从“/var/lib/php/session”更改为“/tmp”):

session.save_handler = files
session.save_path = "/tmp"
session.use_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor     = 1000
session.gc_maxlifetime = 1440
session.bug_compat_42 = 0
session.bug_compat_warn = 1
session.referer_check =
session.entropy_length = 0
session.entropy_file =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5

错误信息是: Fatal error: session_start() [<a href='function.session-start'>function.session-start</a>]: Failed to initialize storage module: user (path: /tmp)

请帮忙,谢谢!

4

1 回答 1

1

最后经过无休止的调试和环境更改,我得到了解决方案!!!

我发现这是php版本的问题。CentOS VPS 默认使用Cent OS 5.5and PHP 5.1.6,但它在支持自定义会话保存处理程序方面存在问题。奇怪的是没有人也没有医生提到这一点!!!我的建议是使用 PHP 5.2.6 或更高版本来使用此类功能。

于 2012-04-17T05:49:51.997 回答