session_test.module 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Implements hook_menu().
  4. */
  5. function session_test_menu() {
  6. $items['session-test/get'] = array(
  7. 'title' => 'Session value',
  8. 'page callback' => '_session_test_get',
  9. 'access arguments' => array('access content'),
  10. 'type' => MENU_CALLBACK,
  11. );
  12. $items['session-test/id'] = array(
  13. 'title' => 'Session ID',
  14. 'page callback' => '_session_test_id',
  15. 'access arguments' => array('access content'),
  16. 'type' => MENU_CALLBACK,
  17. );
  18. $items['session-test/id-from-cookie'] = array(
  19. 'title' => 'Session ID from cookie',
  20. 'page callback' => '_session_test_id_from_cookie',
  21. 'access arguments' => array('access content'),
  22. 'type' => MENU_CALLBACK,
  23. );
  24. $items['session-test/set/%'] = array(
  25. 'title' => 'Set session value',
  26. 'page callback' => '_session_test_set',
  27. 'page arguments' => array(2),
  28. 'access arguments' => array('access content'),
  29. 'type' => MENU_CALLBACK,
  30. );
  31. $items['session-test/no-set/%'] = array(
  32. 'title' => 'Set session value but do not save session',
  33. 'page callback' => '_session_test_no_set',
  34. 'page arguments' => array(2),
  35. 'access arguments' => array('access content'),
  36. 'type' => MENU_CALLBACK,
  37. );
  38. $items['session-test/set-message'] = array(
  39. 'title' => 'Set message',
  40. 'page callback' => '_session_test_set_message',
  41. 'access arguments' => array('access content'),
  42. 'type' => MENU_CALLBACK,
  43. );
  44. $items['session-test/set-message-but-dont-save'] = array(
  45. 'title' => 'Set message but do not save session',
  46. 'page callback' => '_session_test_set_message_but_dont_save',
  47. 'access arguments' => array('access content'),
  48. 'type' => MENU_CALLBACK,
  49. );
  50. $items['session-test/set-not-started'] = array(
  51. 'title' => 'Set message when session is not started',
  52. 'page callback' => '_session_test_set_not_started',
  53. 'access arguments' => array('access content'),
  54. 'type' => MENU_CALLBACK,
  55. );
  56. $items['session-test/is-logged-in'] = array(
  57. 'title' => 'Check if user is logged in',
  58. 'page callback' => '_session_test_is_logged_in',
  59. 'access callback' => 'user_is_logged_in',
  60. 'type' => MENU_CALLBACK,
  61. );
  62. return $items;
  63. }
  64. /**
  65. * Implements hook_boot().
  66. */
  67. function session_test_boot() {
  68. header('X-Session-Empty: ' . intval(empty($_SESSION)));
  69. }
  70. /**
  71. * Page callback, prints the stored session value to the screen.
  72. */
  73. function _session_test_get() {
  74. if (!empty($_SESSION['session_test_value'])) {
  75. return t('The current value of the stored session variable is: %val', array('%val' => $_SESSION['session_test_value']));
  76. }
  77. else {
  78. return "";
  79. }
  80. }
  81. /**
  82. * Page callback, stores a value in $_SESSION['session_test_value'].
  83. */
  84. function _session_test_set($value) {
  85. $_SESSION['session_test_value'] = $value;
  86. return t('The current value of the stored session variable has been set to %val', array('%val' => $value));
  87. }
  88. /**
  89. * Menu callback: turns off session saving and then tries to save a value
  90. * anyway.
  91. */
  92. function _session_test_no_set($value) {
  93. drupal_save_session(FALSE);
  94. _session_test_set($value);
  95. return t('session saving was disabled, and then %val was set', array('%val' => $value));
  96. }
  97. /**
  98. * Menu callback: print the current session ID.
  99. */
  100. function _session_test_id() {
  101. // Set a value in $_SESSION, so that drupal_session_commit() will start
  102. // a session.
  103. $_SESSION['test'] = 'test';
  104. drupal_session_commit();
  105. return 'session_id:' . session_id() . "\n";
  106. }
  107. /**
  108. * Menu callback: print the current session ID as read from the cookie.
  109. */
  110. function _session_test_id_from_cookie() {
  111. return 'session_id:' . $_COOKIE[session_name()] . "\n";
  112. }
  113. /**
  114. * Menu callback, sets a message to me displayed on the following page.
  115. */
  116. function _session_test_set_message() {
  117. drupal_set_message(t('This is a dummy message.'));
  118. print t('A message was set.');
  119. // Do not return anything, so the current request does not result in a themed
  120. // page with messages. The message will be displayed in the following request
  121. // instead.
  122. }
  123. /**
  124. * Menu callback, sets a message but call drupal_save_session(FALSE).
  125. */
  126. function _session_test_set_message_but_dont_save() {
  127. drupal_save_session(FALSE);
  128. _session_test_set_message();
  129. }
  130. /**
  131. * Menu callback, stores a value in $_SESSION['session_test_value'] without
  132. * having started the session in advance.
  133. */
  134. function _session_test_set_not_started() {
  135. if (!drupal_session_will_start()) {
  136. $_SESSION['session_test_value'] = t('Session was not started');
  137. }
  138. }
  139. /**
  140. * Implements hook_user().
  141. */
  142. function session_test_user_login($edit = array(), $user = NULL) {
  143. if ($user->name == 'session_test_user') {
  144. // Exit so we can verify that the session was regenerated
  145. // before hook_user() was called.
  146. exit;
  147. }
  148. }
  149. /**
  150. * Implements hook_form_FORM_ID_alter().
  151. */
  152. function session_test_form_user_login_alter(&$form) {
  153. $form['#https'] = TRUE;
  154. }
  155. /**
  156. * Implements hook_drupal_goto_alter().
  157. *
  158. * Force the redirection to go to a non-secure page after being on a secure
  159. * page through https.php.
  160. */
  161. function session_test_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  162. global $base_insecure_url, $is_https_mock;
  163. // Alter the redirect to use HTTP when using a mock HTTPS request through
  164. // https.php because form submissions would otherwise redirect to a
  165. // non-existent HTTPS site.
  166. if (!empty($is_https_mock)) {
  167. $path = $base_insecure_url . '/' . $path;
  168. }
  169. }
  170. /**
  171. * Menu callback, only available if current user is logged in.
  172. */
  173. function _session_test_is_logged_in() {
  174. return t('User is logged in.');
  175. }