这篇文章主要讲解了“PHP怎么用封装的数据库保存session功能类”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“PHP怎么用封装的数据库保存session功能类”吧!
PHP用数据库保存session类:
<?php class SafeSessionHandler implements SessionHandlerInterface { public $save_path; public $session_name; public $table; public function __construct() { $this->table = new Table("safe_session"); } private function session_id_parse($session_id) { $time = hexdec(substr($session_id, 0, 8)); $skey = substr($session_id, 8); return array($time, $skey); } public function close() { loginfo("close: "); return true; } public function create_sid() { loginfo("create_sid: "); $time = time(); $skey = ""; $char = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; for ($i=0; $i<52; $i++) { $skey .= $char{mt_rand(0, 61)}; } $session = array( "time" => $time, "skey" => $skey, "sval" => "", ); $this->table->insert($session); return dechex($time) . $skey; } public function destroy($session_id) { loginfo("destroy: %s", $session_id); list($time, $skey) = $this->session_id_parse($session_id); $this->table->where("time = ?", $time)->where("skey = ?", $skey)->delete(); return true; } public function gc($maxlifetime) { loginfo("gc: %s", $maxlifetime); $this->table->where("time < ?", time() - 86400 * 30)->delete(); return true; } public function open($save_path, $session_name) { loginfo("open: %s, %s", $save_path, $session_name); $this->save_path = $save_path; $this->session_name = $session_name; return true; } public function read($session_id) { loginfo("read: %s", $session_id); list($time, $skey) = $this->session_id_parse($session_id); $row = $this->table->where("time = ?", $time)->where("skey = ?", $skey)->select()->fetch(); if (empty($row)) { return ""; } return $row["sval"]; } public function write($session_id, $session_data) { loginfo("write: %s, %s", $session_id, $session_data); $session = array("sval" => $session_data,); list($time, $skey) = $this->session_id_parse($session_id); $this->table->where("time = ?", $time)->where("skey = ?", $skey)->update($session); return true; } }
感谢各位的阅读,以上就是“PHP怎么用封装的数据库保存session功能类”的内容了,经过本文的学习后,相信大家对PHP怎么用封装的数据库保存session功能类这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。