本文实例介绍了php session 写入数据库的方法,分享给大家供大家参考,具体内容如下
<"session.gc_maxlifetime"); session_set_save_handler( array($this, "open"), array($this, "close"), array($this, "read"), array($this, "write"), array($this, "destroy"), array($this, "gc")); } public function open() { $this->link = mysqli_connect( $this->config['host'], $this->config['user'], $this->config['password'], $this->config['database']); mysqli_set_charset($this->link, "utf8"); $sql = 'CREATE TABLE IF NOT EXISTS `%s` ( `session_id` varchar(255) NOT NULL, `session_data` text, `session_expires` char(10) NOT NULL, PRIMARY KEY (`session_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;'; $sql = sprintf($sql, $this->config['table']); mysqli_query($this->link, $sql); return true; } public function close() { return true; } public function read($session_id) { if(empty($session_id)) return null; $sql = 'SELECT `session_data` AS `data` FROM `%s` WHERE `session_id` = "%s" AND `session_expires` > "%u"'; $sql = sprintf($sql, mysqli_real_escape_string($this->link, $this->config['table']), mysqli_real_escape_string($this->link, $session_id), time()); $result = mysqli_query($this->link, $sql); $row = mysqli_fetch_assoc($result); return $row['data']; } public function write($session_id, $session_data) { if(empty($session_id)) return null; $newExpires = time() + $this->maxlifetime; $sql = 'REPLACE INTO `%s` SET `session_id` = "%s", `session_data` = "%s", `session_expires` = "%u"'; $sql = sprintf($sql, mysqli_real_escape_string($this->link, $this->config['table']), mysqli_real_escape_string($this->link, $session_id), mysqli_real_escape_string($this->link, $session_data), $newExpires); $result = mysqli_query($this->link, $sql); return mysqli_affected_rows($this->link); } public function destroy($session_id) { $sql = 'DELETE FROM `%s` WHERE `session_id` = "%s"'; $sql = sprintf($sql, mysqli_real_escape_string($this->link, $this->config['table']), mysqli_real_escape_string($this->link, $session_id)); $result = mysqli_query($this->link, $sql); return mysqli_affected_rows($this->link); } public function gc() { $sql = 'DELETE FROM `%s` WHERE `session_expires` < "%u"'; $sql = sprintf($sql, mysqli_real_escape_string($this->link, $this->config['table']), time()); $result = mysqli_query($this->link, $sql); return mysqli_affected_rows($this->link); } } class session { public static $collection = null; public static function open($clean = false, $token = false) { if($clean) ob_end_clean(); if($token) session_id($token); session_start(); self::$collection = $_SESSION; } public static function id() { $num_args = func_num_args(); if($num_args) { $args = func_get_arg(0); return session_id($args); }else{ return session_id(); } } public static function get($name) { return isset($_SESSION[$name]) "host"=> "127.0.0.1", "user"=> "root", "password"=> "123456", "database"=> "test", "charset"=> "utf8", "table"=> "user_session"); session_handler::init($config); session::open(); session::set("profile", array("id"=> 1, "user"=> "haowei", "vip-level"=> 6));
以上就是本文的全部内容,希望对大家的学习有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月20日
2024年11月20日
- 柏菲·珞叔作品集《金色大厅2》限量开盘母带ORMCD[低速原抓WAV+CUE]
- Gareth.T《sad songs(Explicit)》[320K/MP3][29.03MB]
- Gareth.T《sad songs(Explicit)》[FLAC/分轨][152.85MB]
- 证声音乐图书馆《海风摇曳·盛夏爵士曲》[320K/MP3][63.06MB]
- 龚玥《金装龚玥HQCD》头版限量[WAV分轨]
- 李小春《吻别》萨克斯演奏经典[原抓WAV+CUE]
- 齐秦《辉煌30年24K珍藏版》2CD[WAV+CUE]
- 证声音乐图书馆《海风摇曳·盛夏爵士曲》[FLAC/分轨][321.47MB]
- 群星 《世界经典汽车音乐》 [WAV分轨][1G]
- 冷漠.2011 《冷漠的爱DSD》[WAV+CUE][1.2G]
- 陈明《流金岁月精逊【中唱】【WAV+CUE】
- 群星《Jazz-Ladies1-2爵士女伶1-2》HQCD/2CD[原抓WAV+CUE]
- 群星《美女私房歌》(黑胶)[WAV分轨]
- 郑源.2009《试音天碟》24BIT-96KHZ[WAV+CUE][1.2G]
- 飞利浦试音碟 《环球群星监听录》SACD香港版[WAV+CUE][1.1G]