session.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. const expire_min = 6000;
  13. const prefix = 'PHPSESSID';
  14. const session_name = 'PHPSESSID';
  15. private $expire_time;
  16. static public function instance()
  17. {
  18. if(self::$stInstance == NULL) {
  19. self::$stInstance = new session();
  20. }
  21. return self::$stInstance;
  22. }
  23. private function __construct()
  24. {
  25. }
  26. public function init()
  27. {
  28. session_set_save_handler(
  29. array(&$this,'onOpen'),
  30. array(&$this,'onClose'),
  31. array(&$this,'onRead'),
  32. array(&$this,'onWrite'),
  33. array(&$this,'onDestroy'),
  34. array(&$this,'onGc'));
  35. }
  36. public function start()
  37. {
  38. $this->fdestroy = false;
  39. session_start();
  40. }
  41. public function end()
  42. {
  43. // 会触发write 和 close 函数
  44. session_write_close();
  45. foreach($_SESSION as $key=>$value) {
  46. unset($_SESSION[$key]);
  47. }
  48. foreach($_COOKIE as $key=>$value) {
  49. unset($_COOKIE[$key]);
  50. }
  51. foreach($_SERVER as $key=>$value) {
  52. unset($_SERVER[$key]);
  53. }
  54. }
  55. public function destroy() {
  56. $this->fdestroy = true;
  57. //session_destroy();//会触发destroy 和 close 函数
  58. }
  59. public function onOpen() {
  60. //Log::record("onOpen",Log::DEBUG);
  61. return true;
  62. }
  63. public function onRead($rsid)
  64. {
  65. //Log::record("onRead sid={$rsid}",Log::DEBUG);
  66. $sid = $_COOKIE[self::session_name];
  67. //if(empty($sid) || $sid != $rsid)
  68. if(empty($sid))
  69. {
  70. $ret = session_regenerate_id(false);
  71. if($ret) {
  72. dcache($rsid,self::prefix);
  73. $rsid = session_id();
  74. $this->expire_time = time() + self::expire_min * 60;
  75. fcgi_setcookie("PHPSESSID","{$rsid}",$this->expire_time);
  76. }
  77. return '';
  78. }
  79. else
  80. {
  81. $data = rcache($sid,self::prefix,self::session_name);
  82. if(empty($data)) {
  83. dcache($rsid,self::prefix);
  84. session_regenerate_id(false);
  85. $rsid = session_id();
  86. $this->expire_time = time() + self::expire_min * 60;
  87. fcgi_setcookie("PHPSESSID","{$rsid}",$this->expire_time);
  88. return '';
  89. } else {
  90. return $data[self::session_name];
  91. }
  92. }
  93. }
  94. public function onClose() {
  95. return true;
  96. }
  97. public function onWrite($sid, $data) {
  98. if($this->fdestroy) {
  99. dcache($sid,self::prefix);
  100. } else {
  101. wcache($sid,array(self::session_name => $data),self::prefix,self::expire_min);
  102. }
  103. return true;
  104. }
  105. public function onDestroy($sid) {
  106. return dcache($sid,self::prefix);
  107. }
  108. public function onGc($expire) {
  109. return true;
  110. }
  111. }