PageRenderTime 42ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/core/Session.class.php

https://bitbucket.org/stk2k/charcoalphp2.1
PHP | 238 lines | 129 code | 40 blank | 69 comment | 2 complexity | 98b5eeca575064c765add044b424f140 MD5 | raw file
  1. <?php
  2. /**
  3. * ??????????????
  4. *
  5. * PHP version 5
  6. *
  7. * @package core
  8. * @author CharcoalPHP Development Team
  9. * @copyright 2008 - 2012 CharcoalPHP Development Team
  10. */
  11. class Charcoal_Session extends Charcoal_Object
  12. {
  13. var $_data;
  14. /*
  15. * ???????
  16. */
  17. public function __construct()
  18. {
  19. $this->_data = array();
  20. $req_path = Charcoal_Framework::getRequestPath();
  21. log_info( "session", "request_path=" . $req_path );
  22. }
  23. /*
  24. * ?????????
  25. */
  26. public function getName()
  27. {
  28. return session_name();
  29. }
  30. /*
  31. * ????????????
  32. */
  33. public function getSavePath()
  34. {
  35. return session_save_path();
  36. }
  37. /*
  38. * ??????????????????
  39. */
  40. public function getCookieParameter( Charcoal_String $key )
  41. {
  42. $a = session_get_cookie_params();
  43. $key = us( $key );
  44. return $a[ $key ];
  45. }
  46. /*
  47. * ?????????????????????
  48. */
  49. public function getCookieParameters()
  50. {
  51. return session_get_cookie_params();
  52. }
  53. /*
  54. * ???
  55. */
  56. public function clear()
  57. {
  58. $this->_data = array();
  59. log_info( "session", "clear()" );
  60. }
  61. /*
  62. * ID???
  63. */
  64. public function regenerateID()
  65. {
  66. $old_id = session_id();
  67. $result = session_regenerate_id( TRUE );
  68. $new_id = session_id();
  69. log_info( "session", "regenerateID() old=$old_id new=$new_id result=" . ($result ? "TRUE" : "FALSE") );
  70. }
  71. /*
  72. * ???????
  73. */
  74. public function getKeys()
  75. {
  76. return array_keys( $this->_data );
  77. }
  78. /*
  79. * ????????
  80. */
  81. public function get( Charcoal_String $key )
  82. {
  83. $key = us( $key );
  84. $value = isset($this->_data[ $key ]) ? $this->_data[ $key ] : NULL;
  85. log_debug( "session", "get($key)=$value" );
  86. return $value;
  87. }
  88. /*
  89. * ????????
  90. */
  91. public function set( Charcoal_String $key, $value )
  92. {
  93. $key = us( $key );
  94. $this->_data[ $key ] = $value;
  95. log_debug( "session", "set($key," . print_r($value,true) . ")" );
  96. }
  97. /*
  98. * ???????????
  99. */
  100. public function remove( Charcoal_String $key )
  101. {
  102. $key = us( $key );
  103. $value = isset($this->_data[ $key ]) ? $this->_data[ $key ] : NULL;
  104. if ( $value ){
  105. unset( $this->_data[ $key ] );
  106. }
  107. log_debug( "session", "remove($key)=$value" );
  108. return $value;
  109. }
  110. /**
  111. * ??????????
  112. */
  113. public function start()
  114. {
  115. session_cache_limiter('private, must-revalidate');
  116. // session_cache_limiter('private');
  117. // session_cache_limiter('private_no_expire');
  118. // session_cache_limiter('nocache');
  119. session_start();
  120. log_info( "session", "start() session_id=" . session_id() . " IP=" . $_SERVER["REMOTE_ADDR"] );
  121. }
  122. /**
  123. * ??????????
  124. */
  125. public function close()
  126. {
  127. session_write_close();
  128. log_info( "session", "close()" );
  129. }
  130. /*
  131. * ????????
  132. */
  133. public function destroy()
  134. {
  135. $this->clear();
  136. session_unset();
  137. session_destroy();
  138. log_info( "session", "destroy()" );
  139. }
  140. /**
  141. * ??????????
  142. */
  143. public function restore()
  144. {
  145. log_info( "session", "restore() start" );
  146. log_info( "session", "_SESSION:" . print_r($_SESSION,true) );
  147. log_info( "session", "this->_data:" . print_r($this->_data,true) );
  148. // ??????
  149. $this->clear();
  150. log_info( "session", "_SESSION:" . print_r($_SESSION,true) );
  151. // ?????????????
  152. $keys = array_keys( $_SESSION );
  153. if ( $keys ){
  154. foreach( $keys as $key ){
  155. $value = unserialize( $_SESSION[$key] );
  156. $this->set( s($key), $value );
  157. log_info( "session", "key:" . print_r($key,true) );
  158. log_info( "session", "value:" . print_r($value,true) );
  159. }
  160. }
  161. log_info( "session", "_SESSION:" . print_r($_SESSION,true) );
  162. log_info( "session", "this->_data:" . print_r($this->_data,true) );
  163. log_info( "session", "restore() end" );
  164. }
  165. /**
  166. * ??????????
  167. */
  168. public function save()
  169. {
  170. log_info( "session", 'save() start' );
  171. log_info( "session", "_SESSION:" . print_r($_SESSION,true) );
  172. log_info( "session", "this->_data:" . print_r($this->_data,true) );
  173. // ???????
  174. $_SESSION = array();
  175. $keys = $this->getKeys();
  176. log_info( "session", "keys:" . print_r($keys,true) );
  177. // ?????????????????????
  178. foreach( $keys as $key ){
  179. $value = $this->get( s($key) );
  180. $_SESSION[ $key ] = serialize($value);
  181. log_info( "session", "key:" . print_r($key,true) );
  182. log_info( "session", "value:" . print_r($value,true) );
  183. }
  184. log_info( "session", "_SESSION:" . print_r($_SESSION,true) );
  185. log_info( "session", "this->_data:" . print_r($this->_data,true) );
  186. log_info( "session", 'save() end' );
  187. }
  188. /*
  189. * String expression of this object
  190. *
  191. * @return string
  192. */
  193. public function toString()
  194. {
  195. return Charcoal_System::arrayToString( $this->_data );
  196. }
  197. }
  198. return __FILE__;