您好,登錄后才能下訂單哦!
這篇文章主要講解了“php如何基于session實現數據庫交互的類”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“php如何基于session實現數據庫交互的類”吧!
本文實例講述了php基于session實現數據庫交互的類。分享給大家供大家參考。具體如下:
<?php /** * session 數據庫存儲類 */ class Session { private static $session_id = 0; private static $session_data = array(); private static $is_update = FALSE; private static $is_del = FALSE; private static $is_gc = FALSE; private static $dbo = NULL; //數據庫連接句柄 private static $gc_max_time = 1440; private static $table = 'sessions'; private static $pre_key = 'weige';//session 密鑰 //捆綁使用哈 private static $gc_rate_de = 100;//代表分母 private static $gc_rate_co = 20;//代表分子 private static $path = '/';//保存路徑 private static $domain = null; //域 private static $secure = false;//默認 private static $httponly = false;//默認 /** * 獲取數據庫句柄 私有 */ private static function open() { if (!self::$dbo) { self::$dbo = Db::factory(); } return TRUE; } /** * 設置 * */ public static function set($key, $val=NULL) { self::open(); $data = self::read(); if ($data === FALSE) { $data = array(); } if (!$val && is_array($key)) { $data = $key; } else if ($val && is_string($key)) { $data[$key] = $val; } self::write($data); self::close(); } /** *獲取值 * */ public static function get($key=NULL) { self::open(); self::$session_data = self::read(); $ret = ''; if (!$key) { $ret = self::$session_data; } else if(is_array(self::$session_data) && isset(self::$session_data[$key])) { $ret = self::$session_data[$key]; } self::update(); self::close(); return $ret; } /** * 刪除或者重置 * */ public static function del($key) { if (!self::$is_del) { self::open(); $val = self::read(); if (isset($val[$key])) { unset($val[$key]); } $session_id = self::$session_id; $session_data = serialize($val); $session_expire = TIME + self::get_gc_maxtime(); self::$dbo->query("update ".self::$table." set value='$session_data', expiry='$session_expire' where session_id='$session_id'"); self::close(); } self::$is_del = TRUE; } /** * 銷毀 * * */ public static function destroy() { $session_id = self::get_session_id(); $_COOKIE['WBSID'] = ''; self::open(); self::$dbo->query("delete from ".self::$table." where session_id='$session_id'"); self::close(); } /** * 讀取 私有 * */ private static function read() { $session_id = self::$session_id; if (!$session_id) { $session_id = self::get_session_id(); } if (!$session_id) return array(); $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? md5($_SERVER['HTTP_USER_AGENT']) : ''; $client_ip = Fun::getIp(); $session_expire = TIME - self::get_gc_maxtime(); $rs = self::$dbo->fetchRow("select session_id, value, agent, ip from ".self::$table." where session_id='$session_id' and expiry>'$session_expire'"); if (!$rs || $rs['agent'] != $user_agent || $rs['ip'] != $client_ip) { return FALSE; } self::$session_id = $rs['session_id']; return unserialize($rs['value']); } /** * session 寫入 私有 * */ private static function write(array $session_data) { $session_id = self::$session_id; if (!$session_id) { $session_id = self::get_session_id(); } $session_expire = TIME + self::get_gc_maxtime(); $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? md5($_SERVER['HTTP_USER_AGENT']) : ''; $client_ip = Fun::getIp(); $session_data = serialize($session_data); if (self::$session_id && self::$session_id === $session_id) { self::$dbo->query("update ".self::$table." set value='$session_data', expiry='$session_expire', agent='$user_agent', ip='$client_ip' where session_id='$session_id'"); } else { self::$session_id = $session_id = self::create_session_id(); self::$dbo->query("insert into ".self::$table."(session_id, value, expiry, agent, ip) values('$session_id', '$session_data', '$session_expire', '$user_agent', '$client_ip')"); } return true; } /** * session 更新 私有 * */ private static function update() { if (!self::$is_update) { $session_id = self::$session_id; $session_expire = TIME + self::get_gc_maxtime(); self::$dbo->query("update ".self::$table." set expiry='$session_expire' where session_id='$session_id'"); } self::$is_update = TRUE; } private static function close() { if (!self::$is_gc && mt_rand(1, self::$gc_rate_de)%self::$gc_rate_co == 0) { self::gc(); } self::$is_gc = TRUE; } /** * 過期session 清除 隨機觸發 * */ private static function gc() { $session_expire = TIME - self::get_gc_maxtime(); self::$dbo->query("delete from ".self::$table." where expiry<'$session_expire'"); } private static function get_session_id() { if (isset($_COOKIE['WBSID']) && strlen($_COOKIE['WBSID'])==32) { $sid = $_COOKIE['WBSID']; setcookie('WBSID', $sid, TIME + self::get_gc_maxtime(), self::$path, self::$domain, self::$secure, self::$httponly); return $sid; } return null; } private static function create_session_id() { $sid = self::get_session_id(); if (!$sid) { $sid = Fun::getIp() . TIME . microtime(TRUE) . mt_rand(mt_rand(0, 100), mt_rand(100000, 90000000)); $sid = md5(self::$pre_key . $sid); setcookie('WBSID', substr($sid, 0, 32), TIME + self::get_gc_maxtime(), self::$path, self::$domain, self::$secure, self::$httponly); } return $sid; } public static function get_gc_maxtime() { return self::$gc_max_time; } }
感謝各位的閱讀,以上就是“php如何基于session實現數據庫交互的類”的內容了,經過本文的學習后,相信大家對php如何基于session實現數據庫交互的類這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。