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

session保存在mysql

PHP中文网
Lepaskan: 2016-05-25 17:03:58
asal
1005 orang telah melayarinya

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

?>
Salin selepas log masuk

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


Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan