在PHP中自定义session的存储方式
来源:互联网 发布:ubuntu 上传文件 编辑:程序博客网 时间:2024/06/04 20:14
描述
在PHP中可以通过定义session_set_save_handler,将服务器session数据存储在不同的介质上,比如存储在文件里,apc或memcache缓存中,或存储在数据库里。可对统计在线人数,或踢除特定会员的登陆状态等等。
自定义session_set_save_handler,基本上就是使用自定义的读写方法覆盖了系统默认的session的读写方法,以实现对session的管理。欢迎大家一起交流,有什么疑问请写信给我。
工厂类
<?php/** * CHttpSession * http session 数据存储引擎 */class CHttpSession {private static $engine;private static $gc_maxlifetime;public static function engine( $enginer ) {$enginer= ucfirst( $enginer );$engineInstance = "CHttpSession{$enginer}";$filename= SYS_MODULE . '/Session/' . $engineInstance . '.php';if ( !file_exists( $filename )) {throw new Exception( 'Fatal: not found {$filename} file' );}require( $filename );if ( !class_exists( $engineInstance ) ) {throw new Exception( 'Fatal: not found {$engineInstance} object' );}$handler= new CHttpSession( new $engineInstance );ini_set( "session.save_handler", "user" );ini_set( 'apc.ttl', 3600 );ini_set( 'apc.user_ttl', 1200 );ini_set( 'apc.gc_ttl', 3600 );session_set_save_handler(array($handler, 'open'),array($handler, 'close'),array($handler, 'read'),array($handler, 'write'),array($handler, 'destroy'),array($handler, 'gc'));if ( isset( $_COOKIE['PHPSESSID'] ) ) {session_start( $_COOKIE['PHPSESSID'] );}else {session_start( );setcookie( 'PHPSESSID', session_id(), null, '/', COOKIE_DOMAIN );}}public function __construct( & $engine ) {self::$engine = $engine;self::$gc_maxlifetime = ini_get( 'session.gc_maxlifetime' );}public function read( $id ) {return self::$engine->fetch( 'session/'.$id );}public function write ( $id , $data ) {return self::$engine->add( 'session/'.$id, $data, self::$gc_maxlifetime );}public function close ( ) {return true;}public function destroy ( $id ) {return self::$engine->delete( 'session/'.$id );}public function __destruct ( ) {session_write_close();}public function gc ( $maxlifetime ) {return true;}public function open ( $save_path , $session_name ) {return true;}};
具体方法
CHttpSessionFile
<?php/** * CFileHttpSession * session引擎, 以文件的方式对session进行存储, YPi框架默认session存储引擎 * SESSION_DIR 设置session文件存储路径 */class CHttpSessionFile {public function add( $key, $data, $cg_maxlifetime ) {$filepath = substr( $key, 7 );file_put_contents( SESSION_DIR.$filepath, $data );return true;}public function fetch( $key ) {$filepath = substr( $key, 7 );if ( !file_exists(SESSION_DIR.$filepath) ) {file_put_contents( SESSION_DIR.$filepath, '' );return true;}return file_get_contents( SESSION_DIR.$filepath );}public function delete( $key ) {$filepath = substr( $key, 7 );if ( file_exists( SESSION_DIR.$filepath ) ) {unlink( SESSION_DIR.$filepath );}return true;}};
CHttpSessionApc
<?php/** * CApcHttpSession * session引擎, 以APC缓存的方式对session进行存储 * SESSION_ENGINE 设置值为apc,以启用APC方式对session进行存储 */class CHttpSessionApc {public function add( $key, $data, $cg_maxlifetime ) {apc_store( $key, $data, $cg_maxlifetime );return true;}public function fetch( $key ) {if ( !apc_exists( $key ) ) {apc_store( $key, '' );return true;}return apc_fetch( $key );}public function delete( $key ) {if ( apc_exists( $key ) ) {apc_delete( $key );}return true;}};
CHttpSessionMemcache
<?php/** * CMemcacheHttpSession * session引擎, 以memcache缓存的方式对session进行存储 * * SESSION_ENGINE设置值为memcache,以启用memcache方式对session进行存储 * MEMCACHE_HOST设置memcache服务器地址 * MEMCACHE_PORT设置memcache服务器访问端口号 */class CHttpSessionMemcache {private static $memcache;public function __constrct( $config ) {self::$memcache = new Memcache;self::$memcache->connect( MEMCACHE_HOST, MEMCACHE_PORT );}public function __destroy() {self::$memcache->close();}public function add( $key, $data, $cg_maxlifetime ) {return self::$memcache->add( $key, $data, $cg_maxlifetime );}public function fetch( $key ) {return self::$memcache->get( $key );}public function delete( $key ) {return self::$memcache->delete( $key );}};
实例说明
只需要把平时用的 session_start() 替换成以下的方法即可。
<?phpdefined('SESSION_ENGINE')||define('SESSION_ENGINE', 'file');require '../lib/CHttpSession.php';CHttpSession::engine( SESSION_ENGINE );
- 在PHP中自定义session的存储方式
- php自定义session存储方式
- php 实现自定义session存储方式
- PHP session 存储方式
- PHP session 存储方式
- php session 存储方式
- PHP session 存储方式
- php 在JS中获取已存储的session值
- PHP session 存储方式的详细介绍
- Session的存储方式
- session的存储方式
- 在Struts2 中获取session 的方式
- php中 session存储用户信息
- php之自定义session存储机制【3】
- PHP Session 两种不同存储方式
- Session 会话的存储方式
- Session存储在数据中
- 在Struts2中存储session
- 相位相关算法
- DPI深度包检测技术及其作用
- Pictures in Detail
- WINCE下OPGL开发资料
- 进程状态
- 在PHP中自定义session的存储方式
- white-space: nowrap 导致有宽度也不换行的问题
- Matlab图形及GUI
- Windows客户端的JProfiler远程监控Linux上的Tomcat
- 数据库事务级别
- 关于不能调试Server服务代码的解决方法
- STL map常用操作简介
- make install作用
- Tegra 2处理器解析