session.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/1/30
  6. * Time: 下午3:43
  7. */
  8. class session
  9. {
  10. static $stInstance = NULL;
  11. private $fdestroy = false;
  12. private $redis = NULL;
  13. private $enable = false;
  14. const sid_expire = 60;
  15. const type = 'redis';
  16. const save_path = 'tcp://127.0.0.1:6379';
  17. const prefix = 'PHPREDIS_SESSION';
  18. const session_name = 'PHPSESSID';
  19. static public function instance()
  20. {
  21. if(self::$stInstance == NULL) {
  22. self::$stInstance = new session();
  23. }
  24. return self::$stInstance;
  25. }
  26. private function __construct()
  27. {
  28. $this->redis = new Redis;
  29. $this->enable = $this->redis->connect('127.0.0.1', 6379);
  30. }
  31. public function init()
  32. {
  33. // @ini_set("session.save_handler", "redis");
  34. // session_save_path(self::save_path);
  35. session_set_save_handler(
  36. array(&$this,'onOpen'),
  37. array(&$this,'onClose'),
  38. array(&$this,'onRead'),
  39. array(&$this,'onWrite'),
  40. array(&$this,'onDestroy'),
  41. array(&$this,'onGc'));
  42. //array(&$this,'onCreateSid'));
  43. }
  44. public function start()
  45. {
  46. //会触发,open 和 read 函数
  47. session_start();
  48. }
  49. public function end()
  50. {
  51. // 会触发write 和 close 函数
  52. if($this->fdestroy == false) {
  53. session_write_close();
  54. }
  55. foreach($_SESSION as $key=>$value) {
  56. unset($_SESSION[$key]);
  57. }
  58. }
  59. public function destroy() {
  60. $this->fdestroy = true;
  61. session_destroy();//会触发destroy 和 close 函数
  62. }
  63. public function onOpen() {
  64. Log::record("onOpen",Log::DEBUG);
  65. return true;
  66. }
  67. public function onRead($sid)
  68. {
  69. Log::record("onRead sid={$sid}",Log::DEBUG);
  70. $ret = $this->redis->hGet(self::prefix,$sid);
  71. if(empty($ret))
  72. {
  73. Log::record("onRead " . $_COOKIE[self::session_name],Log::DEBUG);
  74. if(empty($_COOKIE[self::session_name]) || $_COOKIE[self::session_name] != $sid) {
  75. Log::record("onRead fcgi_setcookie reset cookie",Log::DEBUG);
  76. fcgi_setcookie("PHPSESSID","{$sid}",time() + self::sid_expire);
  77. }
  78. return '';
  79. } else {
  80. Log::record("onRead 2",Log::DEBUG);
  81. return $ret[0];
  82. }
  83. }
  84. public function onClose() {
  85. Log::record("onClose",Log::DEBUG);
  86. return true;
  87. }
  88. public function onWrite($sid, $data) {
  89. Log::record("onWrite sid={$sid}",Log::DEBUG);
  90. $ret = $this->redis->hSet(self::prefix,$sid,$data);
  91. if($ret) {
  92. $ret = $this->redis->expire(self::prefix . ":" . $sid, self::sid_expire);
  93. }
  94. return true;
  95. }
  96. public function onDestroy($sid) {
  97. Log::record("onDestroy sid={$sid}",Log::DEBUG);
  98. return dcache($sid,self::prefix);
  99. }
  100. public function onGc($expire) {
  101. Log::record("onGc expire={$expire}",Log::DEBUG);
  102. return true;
  103. }
  104. // public function onCreateSid()
  105. // {
  106. // $sid = 'vnk730je4l1c20cc479gpoesm6';
  107. //
  108. // return $sid;
  109. // }
  110. }