save_handler.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. DEFINE("SESSION_FILE_PREFIX" ,"session_test_");
  3. /*
  4. * == General Return Value Rule ==
  5. *
  6. * Returning FALSE indicates FATAL error.
  7. * Exceptions are: gc(), validate_sid()
  8. *
  9. * == Session Data Lock ==
  10. *
  11. * Session data lock is mandatory. Lock must be exclusive. i.e. Block read also.
  12. *
  13. * == Collision Detection ==
  14. *
  15. * Collision detection is mandatory to reject attacker initialized session ID.
  16. * Coolision detection is absolute requirement for secure session.
  17. */
  18. /* Open session data database */
  19. function open($save_path, $session_name) {
  20. // string $save_path - Directory path, connection strings, etc. Default: session.save_path
  21. // string $session_name - Session ID cookie name. Default: session.name
  22. global $session_save_path, $name;
  23. $session_save_path = $save_path;
  24. $name = $session_name;
  25. echo "Open [${session_save_path},${session_name}]\n";
  26. // MUST return bool. Return TRUE for success.
  27. return true;
  28. }
  29. /* Close session data database */
  30. function close() {
  31. // void parameter
  32. // NOTE: This function should unlock session data, if write() does not unlock it.
  33. global $session_save_path, $name;
  34. echo "Close [${session_save_path},${name}]\n";
  35. // MUST return bool. Return TRUE for success.
  36. return true;
  37. }
  38. /* Read session data */
  39. function read($id) {
  40. // string $id - Session ID string
  41. // NOTE: All production session save handler MUST implement "exclusive" lock.
  42. // e.g. Use "serializable transaction isolation level" with RDBMS.
  43. // read() would be the best place for locking for most save handlers.
  44. global $session_save_path, $name, $session_id;
  45. $session_id = $id;
  46. echo "Read [${session_save_path},${id}]\n";
  47. $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id;
  48. // read MUST create file. Otherwise, strict mode will not work
  49. touch($session_file);
  50. // MUST return STRING for successful read().
  51. // Return FALSE only when there is error. i.e. Do not return FALSE
  52. // for non-existing session data for the $id.
  53. return (string) @file_get_contents($session_file);
  54. }
  55. /* Write session data */
  56. function write($id, $session_data) {
  57. // string $id - Session ID string
  58. // string $session_data - Session data string serialized by session serializer.
  59. // NOTE: This function may unlock session data locked by read(). If write() is
  60. // is not suitable place your handler to unlock. Unlock data at close().
  61. global $session_save_path, $name, $session_id;
  62. $session_id = $id;
  63. echo "Write [${session_save_path},${id},${session_data}]\n";
  64. $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id;
  65. if ($fp = fopen($session_file, "w")) {
  66. $return = fwrite($fp, $session_data);
  67. fclose($fp);
  68. return $return === FALSE ? FALSE : TRUE;
  69. }
  70. // MUST return bool. Return TRUE for success.
  71. return false;
  72. }
  73. /* Remove specified session */
  74. function destroy($id) {
  75. // string $id - Session ID string
  76. global $session_save_path, $name;
  77. echo "Destroy [${session_save_path},${id}]\n";
  78. $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id;
  79. unlink($session_file);
  80. // MUST return bool. Return TRUE for success.
  81. // Return FALSE only when there is error. i.e. Do not return FALSE
  82. // for non-existing session data for the $id.
  83. return true;
  84. }
  85. /* Perform garbage collection */
  86. function gc($maxlifetime) {
  87. // long $maxlifetime - GC TTL in seconds. Default: session.gc_maxlifetime
  88. global $session_save_path, $name;
  89. $gc_cnt = 0;
  90. $directory = opendir($session_save_path."/");
  91. $length = strlen(SESSION_FILE_PREFIX);
  92. while (($file = readdir($directory)) !== FALSE) {
  93. $qualified = ($session_save_path."/".$file);
  94. if (is_file($qualified) === TRUE) {
  95. if (substr($file, 0, $length) === SESSION_FILE_PREFIX && (filemtime($qualified) + $maxlifetime <= time() )) {
  96. unlink($qualified);
  97. $gc_cnt++;
  98. }
  99. }
  100. }
  101. closedir($directory);
  102. // SHOULD return long (number of deleted sessions).
  103. // Returning TRUE works also, but it will not report correct number of deleted sessions.
  104. // Return negative value for error. FALSE does not work because it's the same as 0.
  105. return $gc_cnt;
  106. }
  107. /* Create new secure session ID */
  108. function create_sid() {
  109. // void parameter
  110. // NOTE: Defining create_sid() is mandatory because validate_sid() is mandatory for
  111. // security reasons for production save handler.
  112. // PHP 7.1 has session_create_id() for secure session ID generation. Older PHPs
  113. // must generate secure session ID by yourself.
  114. // e.g. hash('sha2', random_bytes(64)) or use /dev/urandom
  115. $id = ('PHPT-'.time());
  116. echo "CreateID [${id}]\n";
  117. // MUST return session ID string.
  118. // Return FALSE for error.
  119. return $id;
  120. }
  121. /* Check session ID collision */
  122. function validate_sid($id) {
  123. // string $id - Session ID string
  124. global $session_save_path, $name;
  125. echo "ValidateID [${session_save_path},${id}]\n";
  126. $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id;
  127. $ret = file_exists($session_file);
  128. // MUST return bool. Return TRUE for collision.
  129. // NOTE: This handler is mandatory for session security.
  130. // All save handlers MUST implement this handler.
  131. // Check session ID collision, return TRUE when it collides.
  132. // Otherwise, return FALSE.
  133. return $ret;
  134. }
  135. /* Update session data access time stamp WITHOUT writing $session_data */
  136. function update($id, $session_data) {
  137. // string $id - Session ID string
  138. // string $session_data - Session data serialized by session serializer
  139. // NOTE: This handler is optional. If your session database cannot
  140. // support time stamp updating, you must not define this.
  141. global $session_save_path, $name;
  142. echo "Update [${session_save_path},${id}]\n";
  143. $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id;
  144. $ret = touch($session_file);
  145. // MUST return bool. Return TRUE for success.
  146. return $ret;
  147. }
  148. function feature() {
  149. /* NOT IMPLEMENTED YET */
  150. /* TYPES: gc, create_sid, use_strict_mode, minimzie_lock, lazy_write
  151. /* VALUES: 0=unknown, 1=supported, 2=partially supported, 3=unsupported */
  152. return array('gc'=>0,
  153. 'create_sid'=>1,
  154. 'use_strict_mode'=>2,
  155. 'minimize_lock'=>3,
  156. 'lazy_write'=>4,
  157. 'invalid'=>5,
  158. 'another invalid'=>6
  159. );
  160. }
  161. ?>