会话存储到文件而不是数据库

时间:2014-02-16 18:23:35

标签: php mysql nginx

提前感谢您的帮助。

我刚将我的网络服务器从apache迁移到nginx。会话信息很高兴以前存储在数据库中,现在由于某种原因他们正在写入/ tmp目录。

以下是设置会话处理程序的代码:

session_write_close();

$sess = new sessionHandler();
session_set_save_handler(array($sess, 'open'),
                             array($sess, 'close'),
                             array($sess, 'read'),
                             array($sess, 'write'),
                             array($sess, 'destroy'),
                             array($sess, 'gc'));
register_shutdown_function('session_write_close');
session_start();
$cookieLifetime = 365 * 24 * 60 * 60;
setcookie(session_name(),session_id(),time()+$cookieLifetime);

这是会话处理程序类:

class appSessionHandler {

private $db;

public function open($save_path, $session_name) {

    $this -> db = new appSql();

    return true;     

}

public function close() {

    $this -> db -> close();

}

function read($id) {

    // Create Memcache Class
    $memcache = new memcacher();
    var_dump('123');

    // Check Memcache
    $memcache->key = 'session-' . $id;
    $memcache->get();

    // If results is in memcache        
    if ($memcache->result != false){

        $res = $memcache->result;

        return $res;

    }

    // Else get the result from sql and update cache
    else{

        $this->open();
        $this->db->sqlQuery = "SELECT data FROM session WHERE id = :id";
        $this->db->params = array('id' => $id);
        $res = $this->db->sqlQuery();   

        if (count($res) >= 1){

            $memcache->input = $res[0]->data;
            $memcache->duration = 2000;
            $memcache->set();               
            return $res[0]->data;

        }

        return '';              

    }               

}

function write($id, $data) {

    $access = time();

    $this->open();      

    // Create Memcache Class
    $memcache = new memcacher();

    // Check Memcache
    $memcache->key = 'session-' . $id;              
    $memcache->input = $data;
    $memcache->duration = 2000;
    $memcache->set();               

    $this->db->sqlQuery = "REPLACE INTO session VALUES (:id, :access, :data)";
    $this->db->params = array('id' => $id, 'access' => $access, 'data' => $data);

    return $this->db->sqlQuery();

}

function destroy($id) {

    // Delete From SQL                          
    $this->open();  
    $this->db->sqlQuery = "DELETE FROM session WHERE id = :id";
    $this->db->params = array('id' => $id);

    // Create Memcache Class
    $memcache = new memcacher();

    // Check Memcache
    $memcache->key = 'session-' . $id;
    $memcache->delete();

    return $this->db->sqlQuery();   

}

function gc($max) {

    $old = time() - $max;

    $this->open();
    $this->db->sqlQuery = "DELETE FROM session WHERE access < :old";
    $this->db->params = array('old' => $old);

    return $this->db->sqlQuery();   

}

public function killUserSession($username){

    $this->open();
    $this->db->sqlQuery = "delete from session where data like('%userID|s:%\" :username %\";first_name|s:%')";
    $this->db->params = array('username' => $username);

    $this->db->sqlQuery();      

  }

}

我迷路了!

0 个答案:

没有答案