SessionComponent.php 5.7 KB

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