SessionComponent.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake
  16. * @subpackage cake.cake.libs.controller.components
  17. * @since CakePHP(tm) v 0.10.0.1232
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('CakeSession', 'Core');
  21. /**
  22. * Session Component.
  23. *
  24. * Session handling from the controller.
  25. *
  26. * @package cake
  27. * @subpackage cake.cake.libs.controller.components
  28. * @link http://book.cakephp.org/view/1310/Sessions
  29. *
  30. */
  31. class SessionComponent extends Component {
  32. /**
  33. * Constructor automatically starts the session.
  34. *
  35. * @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
  36. * @param array $settings Array of configuration settings.
  37. */
  38. public function __construct(ComponentCollection $collection, $settings = array()) {
  39. parent::__construct($collection, $settings);
  40. CakeSession::start();
  41. }
  42. /**
  43. * Get / Set the userAgent
  44. *
  45. * @param string $userAgent Set the userAgent
  46. * @return void
  47. */
  48. public function userAgent($userAgent = null) {
  49. return CakeSession::userAgent($userAgent);
  50. }
  51. /**
  52. * Used to write a value to a session key.
  53. *
  54. * In your controller: $this->Session->write('Controller.sessKey', 'session value');
  55. *
  56. * @param string $name The name of the key your are setting in the session.
  57. * This should be in a Controller.key format for better organizing
  58. * @param string $value The value you want to store in a session.
  59. * @return boolean Success
  60. * @link http://book.cakephp.org/view/1312/write
  61. */
  62. public function write($name, $value = null) {
  63. return CakeSession::write($name, $value);
  64. }
  65. /**
  66. * Used to read a session values for a key or return values for all keys.
  67. *
  68. * In your controller: $this->Session->read('Controller.sessKey');
  69. * Calling the method without a param will return all session vars
  70. *
  71. * @param string $name the name of the session key you want to read
  72. * @return mixed value from the session vars
  73. * @link http://book.cakephp.org/view/1314/read
  74. */
  75. public function read($name = null) {
  76. return CakeSession::read($name);
  77. }
  78. /**
  79. * Wrapper for SessionComponent::del();
  80. *
  81. * In your controller: $this->Session->delete('Controller.sessKey');
  82. *
  83. * @param string $name the name of the session key you want to delete
  84. * @return boolean true is session variable is set and can be deleted, false is variable was not set.
  85. * @link http://book.cakephp.org/view/1316/delete
  86. */
  87. public function delete($name) {
  88. return CakeSession::delete($name);
  89. }
  90. /**
  91. * Used to check if a session variable is set
  92. *
  93. * In your controller: $this->Session->check('Controller.sessKey');
  94. *
  95. * @param string $name the name of the session key you want to check
  96. * @return boolean true is session variable is set, false if not
  97. * @link http://book.cakephp.org/view/1315/check
  98. */
  99. public function check($name) {
  100. return CakeSession::check($name);
  101. }
  102. /**
  103. * Used to determine the last error in a session.
  104. *
  105. * In your controller: $this->Session->error();
  106. *
  107. * @return string Last session error
  108. * @link http://book.cakephp.org/view/1318/error
  109. */
  110. public function error() {
  111. return CakeSession::error();
  112. }
  113. /**
  114. * Used to set a session variable that can be used to output messages in the view.
  115. *
  116. * In your controller: $this->Session->setFlash('This has been saved');
  117. *
  118. * Additional params below can be passed to customize the output, or the Message.[key]
  119. *
  120. * @param string $message Message to be flashed
  121. * @param string $element Element to wrap flash message in.
  122. * @param array $params Parameters to be sent to layout as view variables
  123. * @param string $key Message key, default is 'flash'
  124. * @link http://book.cakephp.org/view/1313/setFlash
  125. */
  126. public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
  127. CakeSession::write('Message.' . $key, compact('message', 'element', 'params'));
  128. }
  129. /**
  130. * Used to renew a session id
  131. *
  132. * In your controller: $this->Session->renew();
  133. *
  134. * @return void
  135. */
  136. public function renew() {
  137. return CakeSession::renew();
  138. }
  139. /**
  140. * Used to check for a valid session.
  141. *
  142. * In your controller: $this->Session->valid();
  143. *
  144. * @return boolean true is session is valid, false is session is invalid
  145. */
  146. public function valid() {
  147. return CakeSession::valid();
  148. }
  149. /**
  150. * Used to destroy sessions
  151. *
  152. * In your controller: $this->Session->destroy();
  153. *
  154. * @return void
  155. * @link http://book.cakephp.org/view/1317/destroy
  156. */
  157. public function destroy() {
  158. return CakeSession::destroy();
  159. }
  160. /**
  161. * Returns Session id
  162. *
  163. * If $id is passed in a beforeFilter, the Session will be started
  164. * with the specified id
  165. *
  166. * @param $id string
  167. * @return string
  168. */
  169. public function id($id = null) {
  170. return CakeSession::id($id);
  171. }
  172. /**
  173. * Returns a bool, whether or not the session has been started.
  174. *
  175. * @return boolean
  176. */
  177. public function started() {
  178. return CakeSession::started();
  179. }
  180. }