Cake/Controller/Component/SessionComponent.php

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