欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

session保存在mysql

程序员文章站 2022-06-13 13:54:08
...
session保存在mysql
<?php
require_once("class_mysql.php");

class session
{
    public $db;
    function session(&$db)
    {
        $this -> db = &$db;
        session_module_name('user');
        session_set_save_handler(
            array(&$this, 'open'),
            array(&$this, 'close'),
            array(&$this, 'read'),
            array(&$this, 'write'),
            array(&$this, 'destroy'),
            array(&$this, 'gc')
            );
        session_start();
    } 
    function unserialize($data_value)
    {
        $vars = preg_split('/([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\|/', $data_value, -1, 
        PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
            );
        for ($i = 0; isset($vars[$i]); $i++)
        {
            $result[$vars[$i++]] = unserialize($vars[$i]);
        } 
        return $result;
    } 
    function open($path, $name)
    {
        return true;
    } 
    function close()
    {
        return true;
    } 
    function read($session_id)
    {
        $session_id = $this -> db -> escape_string($session_id);
        if ($row = $this -> db -> query("select * from `sessions` where `session_id` = '$session_id' limit 1"))
        {
            return $row['data_value'];
        } 
        else
        {
            $this -> db -> query("insert into `sessions` set `session_id` = '$session_id'");
            return "";
        } 
    } 
    function write($session_id, $data_value)
    {
        $data = $this -> unserialize($data_value);
        $session_id = $this -> db -> escape_string($session_id);
        $data_value = $this -> db -> escape_string($data_value);
        $sql = "update `sessions` set ";
        if (isset($data['user_id']))
        {
            $sql .= "`user_id` = '{$data['user_id']}', ";
        } 
        $sql .= "`data_value` = '$data_value', "
         . "`last_visit` = null "
         . "where `session_id` = '$session_id'";
        $this -> db -> query($sql);
        return true;
    } 
    function destroy($session_id)
    {
        $session_id = $this -> db -> escape_string($session_id);
        $this -> db -> query("delete from `sessions` where `session_id` = '$session_id'");
        return true;
    } 
    function gc($lifetime)
    {
       $this -> db -> query("delete from `sessions` 
       where unix_timestamp(now()) - unix_timestamp(`last_visit`) > $lifetime");
        return true;
    } 
    // get sessions by user_id
    function get($user_id)
    {
        $user_id = $this -> db -> escape_string($user_id);
        return $this -> db -> query("select * from `sessions` where `user_id` = '$user_id'");
    } 
    // get sessions list
    function lists($page, $rows)
    {
        if ($page == 0)
        {
            return $this -> db -> query("select * from `sessions` order by `user_id`");
        } 
        else
        {
            $start = ($page - 1) * $rows;
            return $this -> db -> query("select * from `sessions` order by `user_id` limit $start, $rows");
        } 
    } 
} 

?>

以上就是session保存在mysql 的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关标签: session,mysql