SessionComponent.php 5.6 KB

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