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

描述

在PHP中可以通过定义session_set_save_handler,将服务器session数据存储在不同的介质上,比如存储在文件里,apc或memcache缓存中,或存储在数据库里。可对统计在线人数,或踢除特定会员的登陆状态等等。

自定义session_set_save_handler,基本上就是使用自定义的读写方法覆盖了系统默认的session的读写方法,以实现对session的管理。欢迎大家一起交流,有什么疑问请写信给我。给我写信

 

工厂类

 

 
  1. <?php

  2.  
  3. /**

  4. * CHttpSession

  5. * http session 数据存储引擎

  6. */

  7.  
  8. class CHttpSession {

  9.  
  10. private static $engine;

  11. private static $gc_maxlifetime;

  12.  
  13. public static function engine( $enginer ) {

  14. $enginer = ucfirst( $enginer );

  15. $engineInstance = "CHttpSession{$enginer}";

  16. $filename = SYS_MODULE . '/Session/' . $engineInstance . '.php';

  17.  
  18. if ( !file_exists( $filename )) {

  19. throw new Exception( 'Fatal: not found {$filename} file' );

  20. }

  21. require( $filename );

  22.  
  23. if ( !class_exists( $engineInstance ) ) {

  24. throw new Exception( 'Fatal: not found {$engineInstance} object' );

  25. }

  26.  
  27. $handler = new CHttpSession( new $engineInstance );

  28.  
  29. ini_set( "session.save_handler", "user" );

  30. ini_set( 'apc.ttl', 3600 );

  31. ini_set( 'apc.user_ttl', 1200 );

  32. ini_set( 'apc.gc_ttl', 3600 );

  33.  
  34. session_set_save_handler(

  35. array($handler, 'open'),

  36. array($handler, 'close'),

  37. array($handler, 'read'),

  38. array($handler, 'write'),

  39. array($handler, 'destroy'),

  40. array($handler, 'gc')

  41. );

  42.  
  43. if ( isset( $_COOKIE['PHPSESSID'] ) ) {

  44. session_start( $_COOKIE['PHPSESSID'] );

  45. }

  46. else {

  47. session_start( );

  48. setcookie( 'PHPSESSID', session_id(), null, '/', COOKIE_DOMAIN );

  49. }

  50. }

  51.  
  52. public function __construct( & $engine ) {

  53. self::$engine = $engine;

  54. self::$gc_maxlifetime = ini_get( 'session.gc_maxlifetime' );

  55. }

  56.  
  57. public function read( $id ) {

  58. return self::$engine->fetch( 'session/'.$id );

  59. }

  60.  
  61. public function write ( $id , $data ) {

  62. return self::$engine->add( 'session/'.$id, $data, self::$gc_maxlifetime );

  63. }

  64.  
  65. public function close ( ) {

  66. return true;

  67. }

  68.  
  69.  
  70. public function destroy ( $id ) {

  71. return self::$engine->delete( 'session/'.$id );

  72. }

  73.  
  74. public function __destruct ( ) {

  75. session_write_close();

  76. }

  77.  
  78.  
  79. public function gc ( $maxlifetime ) {

  80. return true;

  81. }

  82.  
  83.  
  84. public function open ( $save_path , $session_name ) {

  85. return true;

  86. }

  87. };

 

 

具体方法

CHttpSessionFile

 

 
  1. <?php

  2.  
  3. /**

  4. * CFileHttpSession

  5. * session引擎, 以文件的方式对session进行存储, YPi框架默认session存储引擎

  6. * SESSION_DIR 设置session文件存储路径

  7. */

  8.  
  9.  
  10. class CHttpSessionFile {

  11.  
  12. public function add( $key, $data, $cg_maxlifetime ) {

  13. $filepath = substr( $key, 7 );

  14. file_put_contents( SESSION_DIR.$filepath, $data );

  15. return true;

  16. }

  17.  
  18. public function fetch( $key ) {

  19. $filepath = substr( $key, 7 );

  20. if ( !file_exists(SESSION_DIR.$filepath) ) {

  21. file_put_contents( SESSION_DIR.$filepath, '' );

  22. return true;

  23. }

  24. return file_get_contents( SESSION_DIR.$filepath );

  25. }

  26.  
  27. public function delete( $key ) {

  28. $filepath = substr( $key, 7 );

  29. if ( file_exists( SESSION_DIR.$filepath ) ) {

  30. unlink( SESSION_DIR.$filepath );

  31. }

  32. return true;

  33. }

  34. };

 

 

 

CHttpSessionApc

 

 
  1. <?php

  2.  
  3. /**

  4. * CApcHttpSession

  5. * session引擎, 以APC缓存的方式对session进行存储

  6. * SESSION_ENGINE 设置值为apc,以启用APC方式对session进行存储

  7. */

  8.  
  9.  
  10. class CHttpSessionApc {

  11.  
  12. public function add( $key, $data, $cg_maxlifetime ) {

  13. apc_store( $key, $data, $cg_maxlifetime );

  14. return true;

  15. }

  16.  
  17. public function fetch( $key ) {

  18. if ( !apc_exists( $key ) ) {

  19. apc_store( $key, '' );

  20. return true;

  21. }

  22. return apc_fetch( $key );

  23. }

  24.  
  25. public function delete( $key ) {

  26. if ( apc_exists( $key ) ) {

  27. apc_delete( $key );

  28. }

  29. return true;

  30. }

  31. };

 

 

CHttpSessionMemcache

 

 
  1. <?php

  2.  
  3. /**

  4. * CMemcacheHttpSession

  5. * session引擎, 以memcache缓存的方式对session进行存储 *

  6. * SESSION_ENGINE 设置值为memcache,以启用memcache方式对session进行存储

  7. * MEMCACHE_HOST 设置memcache服务器地址

  8. * MEMCACHE_PORT 设置memcache服务器访问端口号

  9. */

  10.  
  11.  
  12. class CHttpSessionMemcache {

  13.  
  14. private static $memcache;

  15.  
  16. public function __constrct( $config ) {

  17. self::$memcache = new Memcache;

  18. self::$memcache->connect( MEMCACHE_HOST, MEMCACHE_PORT );

  19. }

  20.  
  21. public function __destroy() {

  22. self::$memcache->close();

  23. }

  24.  
  25. public function add( $key, $data, $cg_maxlifetime ) {

  26. return self::$memcache->add( $key, $data, $cg_maxlifetime );

  27. }

  28.  
  29. public function fetch( $key ) {

  30. return self::$memcache->get( $key );

  31. }

  32.  
  33. public function delete( $key ) {

  34. return self::$memcache->delete( $key );

  35. }

  36. };


 

 

实例说明

只需要把平时用的 session_start() 替换成以下的方法即可。

 

 
  1. <?php

  2.  
  3. defined('SESSION_ENGINE')||define('SESSION_ENGINE', 'file');

  4. require '../lib/CHttpSession.php';

  5.  
  6. CHttpSession::engine( SESSION_ENGINE );

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值