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