Native.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Native PHP session class.
  4. *
  5. * @package KO7
  6. * @category Session
  7. *
  8. * @copyright (c) 2007-2016 Kohana Team
  9. * @copyright (c) since 2016 Koseven Team
  10. * @license https://koseven.dev/LICENSE
  11. */
  12. class KO7_Session_Native extends Session {
  13. /**
  14. * @return string
  15. */
  16. public function id()
  17. {
  18. return session_id();
  19. }
  20. /**
  21. * @param string $id session id
  22. * @return null
  23. */
  24. protected function _read($id = NULL)
  25. {
  26. /**
  27. * session_set_cookie_params will override php ini settings
  28. * If Cookie::$domain is NULL or empty and is passed, PHP
  29. * will override ini and sent cookies with the host name
  30. * of the server which generated the cookie
  31. *
  32. * see issue #3604
  33. *
  34. * see http://www.php.net/manual/en/function.session-set-cookie-params.php
  35. * see http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-domain
  36. *
  37. * set to Cookie::$domain if available, otherwise default to ini setting
  38. */
  39. $session_cookie_domain = empty(Cookie::$domain)
  40. ? ini_get('session.cookie_domain')
  41. : Cookie::$domain;
  42. // Sync up the session cookie with Cookie parameters
  43. session_set_cookie_params(
  44. $this->_lifetime,
  45. Cookie::$path,
  46. $session_cookie_domain,
  47. Cookie::$secure,
  48. Cookie::$httponly
  49. );
  50. // Do not allow PHP to send Cache-Control headers
  51. session_cache_limiter(FALSE);
  52. // Set the session cookie name
  53. session_name($this->_name);
  54. if ($id)
  55. {
  56. // Set the session id
  57. session_id($id);
  58. }
  59. // Start the session
  60. try {
  61. session_start();
  62. } catch(Exception $e) {
  63. $this->_destroy();
  64. session_start();
  65. }
  66. // Use the $_SESSION global for storing data
  67. $this->_data =& $_SESSION;
  68. return NULL;
  69. }
  70. /**
  71. * @return string
  72. */
  73. protected function _regenerate()
  74. {
  75. // Regenerate the session id
  76. session_regenerate_id();
  77. return session_id();
  78. }
  79. /**
  80. * @return bool
  81. */
  82. protected function _write()
  83. {
  84. // Write and close the session
  85. session_write_close();
  86. return TRUE;
  87. }
  88. /**
  89. * @return bool
  90. */
  91. protected function _restart()
  92. {
  93. // Fire up a new session
  94. $status = session_start();
  95. // Use the $_SESSION global for storing data
  96. $this->_data =& $_SESSION;
  97. return $status;
  98. }
  99. /**
  100. * @return bool
  101. */
  102. protected function _destroy()
  103. {
  104. // Destroy the current session
  105. session_destroy();
  106. // Did destruction work?
  107. $status = ! session_id();
  108. if ($status)
  109. {
  110. // Make sure the session cannot be restarted
  111. Cookie::delete($this->_name);
  112. }
  113. return $status;
  114. }
  115. }