首页 > 编程 > PHP > 正文

在PHP中自定义session的存储方式

2020-03-22 17:37:29
字体:
来源:转载
供稿:网友
  • 在PHP中可以通过定义session_set_save_handler,将服务器session数据存储在不同的介质上,比如存储在文件里,apc或memcache缓存中,或存储在数据库里。可对统计在线人数,或踢除特定会员的登陆状态等等。

    自定义session_set_save_handler,基本上就是使用自定义的读写方法覆盖了系统默认的session的读写方法,以实现对session的管理。

    工厂类


    <?php/** * CHttpSession * http session 数据存储引擎 */html' target='_blank'>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编程

    郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表