您好,登錄后才能下訂單哦!
這篇文章主要講解了“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。