栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在PHP中的数据库中设置会话

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在PHP中的数据库中设置会话

您将需要创建一个像这样的对象:

class SessionHandler {     private static $lifetime = 0;    private function __construct() //object constructor    {        session_set_save_handler(array($this,'open'),array($this,'close'),array($this,'read'),array($this,'write'),array($this,'destroy'),array($this,'gc')       );    }   public function start($session_name = null)   {       session_start($session_name); //Start it here   }    public static function open()    {        //Connect to mysql, if already connected, check the connection state here.        return true;    }    public static function read($id)    {        //Get data from DB with id = $id;    }    public static function write($id, $data)    {        //insert data to DB, take note of serialize    }    public static function destroy($id)    {       //MySql delete sessions where ID = $id    }    public static function gc()    {        return true;    }    public static function close()    {        return true;    }    public function __destruct()    {        session_write_close();    }}

然后,在session_start之前,初始化此类!

include 'classes/sessionHandlerDB.php';$session = new SessionHandler();$session->start('userbase');$_SESSION['name'] = 'Robert Pitt'; //This is sent to SessionHandler::write('my_id','Robert Pitt')echo $_SESSION['name']; //This calls SessionHandler::read($id)//$id is Unique Identifier for that

http://php.net/manual/zh/function.session-set-save-
handler.php

http://php.net/manual/zh/function.serialize.php



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/375656.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号