Native.php 2.5 KB

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