Home >php教程 >PHP源码 >session保存在mysql

session保存在mysql

PHP中文网
PHP中文网Original
2016-05-25 17:03:581005browse

session保存在mysql 

<?php
require_once("class_mysql.php");

class session
{
    public $db;
    function session(&$db)
    {
        $this -> db = &$db;
        session_module_name(&#39;user&#39;);
        session_set_save_handler(
            array(&$this, &#39;open&#39;),
            array(&$this, &#39;close&#39;),
            array(&$this, &#39;read&#39;),
            array(&$this, &#39;write&#39;),
            array(&$this, &#39;destroy&#39;),
            array(&$this, &#39;gc&#39;)
            );
        session_start();
    } 
    function unserialize($data_value)
    {
        $vars = preg_split(&#39;/([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\|/&#39;, $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` = &#39;$session_id&#39; limit 1"))
        {
            return $row[&#39;data_value&#39;];
        } 
        else
        {
            $this -> db -> query("insert into `sessions` set `session_id` = &#39;$session_id&#39;");
            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[&#39;user_id&#39;]))
        {
            $sql .= "`user_id` = &#39;{$data[&#39;user_id&#39;]}&#39;, ";
        } 
        $sql .= "`data_value` = &#39;$data_value&#39;, "
         . "`last_visit` = null "
         . "where `session_id` = &#39;$session_id&#39;";
        $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` = &#39;$session_id&#39;");
        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` = &#39;$user_id&#39;");
    } 
    // 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)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn