SessionComponent.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * SessionComponent. Provides access to Sessions from the Controller layer
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Controller.Component
  15. * @since CakePHP(tm) v 0.10.0.1232
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Component', 'Controller');
  19. App::uses('CakeSession', 'Model/Datasource');
  20. /**
  21. * The CakePHP SessionComponent provides a way to persist client data between
  22. * page requests. It acts as a wrapper for the `$_SESSION` as well as providing
  23. * convenience methods for several `$_SESSION` related functions.
  24. *
  25. * @package Cake.Controller.Component
  26. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
  27. * @link http://book.cakephp.org/2.0/en/development/sessions.html
  28. */
  29. class SessionComponent extends Component {
  30. /**
  31. * Get / Set the userAgent
  32. *
  33. * @param string $userAgent Set the userAgent
  34. * @return void
  35. */
  36. public function userAgent($userAgent = null) {
  37. return CakeSession::userAgent($userAgent);
  38. }
  39. /**
  40. * Used to write a value to a session key.
  41. *
  42. * In your controller: $this->Session->write('Controller.sessKey', 'session value');
  43. *
  44. * @param string $name The name of the key your are setting in the session.
  45. * This should be in a Controller.key format for better organizing
  46. * @param string $value The value you want to store in a session.
  47. * @return bool Success
  48. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::write
  49. */
  50. public function write($name, $value = null) {
  51. return CakeSession::write($name, $value);
  52. }
  53. /**
  54. * Used to read a session values for a key or return values for all keys.
  55. *
  56. * In your controller: $this->Session->read('Controller.sessKey');
  57. * Calling the method without a param will return all session vars
  58. *
  59. * @param string $name the name of the session key you want to read
  60. * @return mixed value from the session vars
  61. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::read
  62. */
  63. public function read($name = null) {
  64. return CakeSession::read($name);
  65. }
  66. /**
  67. * Wrapper for SessionComponent::del();
  68. *
  69. * In your controller: $this->Session->delete('Controller.sessKey');
  70. *
  71. * @param string $name the name of the session key you want to delete
  72. * @return bool true is session variable is set and can be deleted, false is variable was not set.
  73. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::delete
  74. */
  75. public function delete($name) {
  76. return CakeSession::delete($name);
  77. }
  78. /**
  79. * Used to check if a session variable is set
  80. *
  81. * In your controller: $this->Session->check('Controller.sessKey');
  82. *
  83. * @param string $name the name of the session key you want to check
  84. * @return bool true is session variable is set, false if not
  85. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::check
  86. */
  87. public function check($name) {
  88. return CakeSession::check($name);
  89. }
  90. /**
  91. * Used to determine the last error in a session.
  92. *
  93. * In your controller: $this->Session->error();
  94. *
  95. * @return string Last session error
  96. */
  97. public function error() {
  98. return CakeSession::error();
  99. }
  100. /**
  101. * Used to set a session variable that can be used to output messages in the view.
  102. *
  103. * In your controller: $this->Session->setFlash('This has been saved');
  104. *
  105. * Additional params below can be passed to customize the output, or the Message.[key].
  106. * You can also set additional parameters when rendering flash messages. See SessionHelper::flash()
  107. * for more information on how to do that.
  108. *
  109. * @param string $message Message to be flashed
  110. * @param string $element Element to wrap flash message in.
  111. * @param array $params Parameters to be sent to layout as view variables
  112. * @param string $key Message key, default is 'flash'
  113. * @return void
  114. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#creating-notification-messages
  115. */
  116. public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
  117. CakeSession::write('Message.' . $key, compact('message', 'element', 'params'));
  118. }
  119. /**
  120. * Used to renew a session id
  121. *
  122. * In your controller: $this->Session->renew();
  123. *
  124. * @return void
  125. */
  126. public function renew() {
  127. return CakeSession::renew();
  128. }
  129. /**
  130. * Used to check for a valid session.
  131. *
  132. * In your controller: $this->Session->valid();
  133. *
  134. * @return bool true is session is valid, false is session is invalid
  135. */
  136. public function valid() {
  137. return CakeSession::valid();
  138. }
  139. /**
  140. * Used to destroy sessions
  141. *
  142. * In your controller: $this->Session->destroy();
  143. *
  144. * @return void
  145. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::destroy
  146. */
  147. public function destroy() {
  148. return CakeSession::destroy();
  149. }
  150. /**
  151. * Get/Set the session id.
  152. *
  153. * When fetching the session id, the session will be started
  154. * if it has not already been started. When setting the session id,
  155. * the session will not be started.
  156. *
  157. * @param string $id Id to use (optional)
  158. * @return string The current session id.
  159. */
  160. public function id($id = null) {
  161. if (empty($id)) {
  162. CakeSession::start();
  163. }
  164. return CakeSession::id($id);
  165. }
  166. /**
  167. * Returns a bool, whether or not the session has been started.
  168. *
  169. * @return bool
  170. */
  171. public function started() {
  172. return CakeSession::started();
  173. }
  174. }