SESSION存放在数据库用法实例
发布:smiling 来源: PHP粉丝网 添加日期:2021-06-16 10:07:57 浏览: 评论:0
这篇文章主要介绍了SESSION存放在数据库用法,自定义了一个简单的针对数据操作的session类并给出了使用该类存储到数据库的相关技巧,需要的朋友可以参考下
本文实例讲述了SESSION存放在数据库用法,分享给大家供大家参考,具体如下:
- <?php
- /*
- CREATE TABLE `ws_sessions` (
- `session_id` varchar(255) binary NOT NULL default '',
- `session_expires` int(10) unsigned NOT NULL default '0',
- `session_data` text,
- PRIMARY KEY (`session_id`)
- ) TYPE=InnoDB;
- */
- class session {
- // session-lifetime
- var $lifeTime;
- // mysql-handle
- var $dbHandle;
- function open($savePath, $sessName) {
- // get session-lifetime
- $this->lifeTime = get_cfg_var("session.gc_maxlifetime");
- // open database-connection
- $dbHandle = @mysql_connect("localhost","root","");
- $dbSel = @mysql_select_db("test",$dbHandle);
- // return success
- if(!$dbHandle || !$dbSel)
- return false;
- $this->dbHandle = $dbHandle;
- return true;
- }
- function close() {
- $this->gc(ini_get('session.gc_maxlifetime'));
- // close database-connection
- return @mysql_close($this->dbHandle);
- }
- function read($sessID) {
- // fetch session-data
- $res = mysql_query("SELECT session_data AS d FROM ws_sessions
- WHERE session_id = '$sessID'
- AND session_expires > ".time(),$this->dbHandle);
- // return data or an empty string at failure
- if($row = mysql_fetch_assoc($res))
- return $row['d'];
- return "";
- }
- function write($sessID,$sessData) {
- // new session-expire-time
- $newExp = time() + $this->lifeTime;
- // is a session with this id in the database?
- $res = mysql_query("SELECT * FROM ws_sessions
- WHERE session_id = '$sessID'",$this->dbHandle);
- // if yes,
- if(mysql_num_rows($res)) {
- // ...update session-data
- mysql_query("UPDATE ws_sessions
- SET session_expires = '$newExp',
- session_data = '$sessData'
- WHERE session_id = '$sessID'",$this->dbHandle);
- // if something happened, return true
- if(mysql_affected_rows($this->dbHandle))
- return true;
- }
- // if no session-data was found,
- else {
- // create a new row
- mysql_query("INSERT INTO ws_sessions (
- session_id,
- session_expires,
- session_data)
- VALUES(
- '$sessID',
- '$newExp',
- '$sessData')",$this->dbHandle);
- // if row was created, return true
- if(mysql_affected_rows($this->dbHandle))
- return true;
- }
- // an unknown error occured
- return false;
- }
- function destroy($sessID) {
- // delete session-data
- mysql_query("DELETE FROM ws_sessions WHERE session_id = '$sessID'",$this->dbHandle);
- // if session was deleted, return true,
- if(mysql_affected_rows($this->dbHandle))
- return true;
- // ...else return false
- return false;
- }
- function gc($sessMaxLifeTime) {
- // delete old sessions
- mysql_query("DELETE FROM ws_sessions WHERE session_expires < ".time(),$this->dbHandle);
- // return affected rows
- return mysql_affected_rows($this->dbHandle);
- }
- }
- $session = new session();
- session_set_save_handler(array(&$session,"open"),
- array(&$session,"close"),
- array(&$session,"read"),
- array(&$session,"write"),
- array(&$session,"destroy"),
- array(&$session,"gc"));
- session_start();
- // etc...
- ?>
希望本文所述对大家的php程序设计有所帮助。
Tags: SESSION数据库
相关文章
- ·php基于session实现数据库交互的类实例(2021-06-15)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)