object- and mysql-based session-handler

object- and mysql-based session-handler, requires the following table:

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;

<?php 
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("server","user","password"); 
       $dbSel = @mysql_select_db("database",$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();

时间: 2024-08-23 20:27:40

object- and mysql-based session-handler的相关文章

Can&#39;t get WebApplicationContext object from ContextRegistry.GetContext(): Resource handler for the &#39;web&#39; protocol is not defined

I'm stucked in configuring my web.config file under a web forms project in order to get an instance of WebApplicationContext (at Global.asax) and then being able to use scope="application | session | request" <sectionGroup name="spring&q

PHP利用MySQL保存session

实现环境: PHP 5.4.24 MySQL 5.6.19 OS X 10.9.4/Apache 2.2.26 一.代码 CREATE TABLE `session` ( `skey` char(32) CHARACTER SET ascii NOT NULL, `data` text COLLATE utf8mb4_bin, `expire` int(11) NOT NULL, PRIMARY KEY (`skey`), KEY `index_session_expire` (`expire`

使用MySql保存session

本文来源于:http://www.lai18.com/content/433951.html 本文实例讲述了php使用MySQL保存session会话的方法.分享给大家供大家参考.具体分析如下: 在很多大的系统中一般都有这个功能,但是要分离出来分析,网上的资料也不太多 这里我整理了一篇发出来与大家分享 使用MySQL保存session会话较files有很多优点: 1) 有利于分布式系统,files只能保存在一台机器上 2) 有利于大访问量的系统,使用files时每个session保存在一个文件中

redis缓存服务器(Nginx+Tomcat+redis+MySQL实现session会话共享)

一.redis介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set --有序集合)和hash(哈希类型).与memcached一样,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现master-slave(主从)同步. Redis是一个高性能的key-valu

图文并茂超详细搭建redis缓存服务器(nginx+tomcat+redis+mysql实现session会话共享)

博主QQ:819594300 博客地址:http://zpf666.blog.51cto.com/ 有什么疑问的朋友可以联系博主,博主会帮你们解答,谢谢支持! 一.redis介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set --有序集合)和hash(哈希类型).与memcached一样,为了保证效率,数据都是缓存在内存中.区别的是redis会

WAMP(windows+apache+mysql+php)

安装以及配置网络上有图. 特别说明php,ini文件位于C:windows下 内容为: //以下有可能多开了几个ext,只需在不用的ext前面加上:即可. [PHP] ;;;;;;;;;;;;;;;;;;;; About php.ini   ;;;;;;;;;;;;;;;;;;;;; PHP's initialization file, generally called php.ini, is responsible for; configuring many of the aspects of

Build your first web service with PHP, JSON and MySql

原文连接: https://trinitytuts.com/build-first-web-service-php/ Web services ( application services ) is one of the most important part of today development where we ceneteralized or data and allow user to access that data from different sources like web,

MySQL 5.6 Reference Manual-14.6 InnoDB Table Management

14.6 InnoDB Table Management 14.6.1 Creating InnoDB Tables 14.6.2 Moving or Copying InnoDB Tables to Another Machine 14.6.3 Grouping DML Operations with Transactions 14.6.4 Converting Tables from MyISAM to InnoDB 14.6.5 AUTO_INCREMENT Handling in Inn

mysql之Xtrabackup备份及增量备份

Xtrabackup 是由percona提供的mysql数据库备份工具,是一个开源的工具,能够对innodb和xtradb数据库进行热备和增量备份,对于MyISAM, 仅支持到温备,对MyISAM使用增量备份时,其实是完全备份.Xtrabackup使用简单,功能强大. 官方地址:http://www.percona.com/software/percona-xtrabackup/ 安装: percona-toolkit-2.2.17-1.noarch.rpm percona-xtrabackup

00 MySQL

1数据库 1.1名词解释 DB:数据库Database,用于存放数据仓库 DBMS:数据库管理系统 DataBase Management System,管理数据库 table : 表,用于描述实体(对象)集合,需要提供行和列 1.2数据库分类 网状型数据库 层次型数据库 关系型数据库(Relationship DataBase Management System) 非关系型数据库:NoSql (not only sql) 1.3关系型数据库种类 DB2 : IBM公司,收费 Oracle :