SessionComponent.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.libs.controller.components
  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. * Session Component.
  23. *
  24. * Session handling from the controller.
  25. *
  26. * @package cake.libs.controller.components
  27. * @link http://book.cakephp.org/view/1310/Sessions
  28. *
  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/view/1312/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/view/1314/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/view/1316/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/view/1315/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. * @link http://book.cakephp.org/view/1318/error
  98. */
  99. public function error() {
  100. return CakeSession::error();
  101. }
  102. /**
  103. * Used to set a session variable that can be used to output messages in the view.
  104. *
  105. * In your controller: $this->Session->setFlash('This has been saved');
  106. *
  107. * Additional params below can be passed to customize the output, or the Message.[key]
  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. * @link http://book.cakephp.org/view/1313/setFlash
  114. */
  115. public function setFlash($message, $element = 'default', $params = array(), $key = 'flash') {
  116. CakeSession::write('Message.' . $key, compact('message', 'element', 'params'));
  117. }
  118. /**
  119. * Used to renew a session id
  120. *
  121. * In your controller: $this->Session->renew();
  122. *
  123. * @return void
  124. */
  125. public function renew() {
  126. return CakeSession::renew();
  127. }
  128. /**
  129. * Used to check for a valid session.
  130. *
  131. * In your controller: $this->Session->valid();
  132. *
  133. * @return boolean true is session is valid, false is session is invalid
  134. */
  135. public function valid() {
  136. return CakeSession::valid();
  137. }
  138. /**
  139. * Used to destroy sessions
  140. *
  141. * In your controller: $this->Session->destroy();
  142. *
  143. * @return void
  144. * @link http://book.cakephp.org/view/1317/destroy
  145. */
  146. public function destroy() {
  147. return CakeSession::destroy();
  148. }
  149. /**
  150. * Returns Session id
  151. *
  152. * If $id is passed in a beforeFilter, the Session will be started
  153. * with the specified id
  154. *
  155. * @param $id string
  156. * @return string
  157. */
  158. public function id($id = null) {
  159. return CakeSession::id($id);
  160. }
  161. /**
  162. * Returns a bool, whether or not the session has been started.
  163. *
  164. * @return boolean
  165. */
  166. public function started() {
  167. return CakeSession::started();
  168. }
  169. }