session.php
Go to the documentation of this file.00001 <?php 00002 /* SVN FILE: $Id: controller_2components_2session_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */ 00003 /** 00004 * Short description for file. 00005 * 00006 * Long description for file 00007 * 00008 * PHP versions 4 and 5 00009 * 00010 * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> 00011 * Copyright 2005-2008, Cake Software Foundation, Inc. 00012 * 1785 E. Sahara Avenue, Suite 490-204 00013 * Las Vegas, Nevada 89104 00014 * 00015 * Licensed under The MIT License 00016 * Redistributions of files must retain the above copyright notice. 00017 * 00018 * @filesource 00019 * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. 00020 * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project 00021 * @package cake 00022 * @subpackage cake.cake.libs.controller.components 00023 * @since CakePHP(tm) v 0.10.0.1232 00024 * @version $Revision: 675 $ 00025 * @modifiedby $LastChangedBy: gwoo $ 00026 * @lastmodified $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $ 00027 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 00028 */ 00029 /** 00030 * Session Component. 00031 * 00032 * Session handling from the controller. 00033 * 00034 * @package cake 00035 * @subpackage cake.cake.libs.controller.components 00036 * 00037 */ 00038 class SessionComponent extends CakeSession { 00039 /** 00040 * Used to determine if methods implementation is used, or bypassed 00041 * 00042 * @var boolean 00043 * @access private 00044 */ 00045 var $__active = true; 00046 /** 00047 * Used to determine if Session has been started 00048 * 00049 * @var boolean 00050 * @access private 00051 */ 00052 var $__started = false; 00053 /** 00054 * Used to determine if request are from an Ajax request 00055 * 00056 * @var boolean 00057 * @access private 00058 */ 00059 var $__bare = 0; 00060 /** 00061 * Class constructor 00062 * 00063 * @param string $base The base path for the Session 00064 */ 00065 function __construct($base = null) { 00066 if (!defined('AUTO_SESSION') || AUTO_SESSION === true) { 00067 parent::__construct($base); 00068 } else { 00069 $this->__active = false; 00070 } 00071 } 00072 /** 00073 * Initializes the component, gets a reference to Controller::$param['bare']. 00074 * 00075 * @param object $controller A reference to the controller 00076 * @access public 00077 */ 00078 function initialize(&$controller) { 00079 if (isset($controller->params['bare'])) { 00080 $this->__bare = $controller->params['bare']; 00081 } 00082 } 00083 /** 00084 * Startup method. 00085 * 00086 * @param object $controller Instantiating controller 00087 * @access public 00088 */ 00089 function startup(&$controller) { 00090 if ($this->__started === false && $this->__active === true) { 00091 $this->__start(); 00092 } 00093 } 00094 /** 00095 * Starts Session on if 'Session.start' is set to false in core.php 00096 * 00097 * @param string $base The base path for the Session 00098 * @access public 00099 */ 00100 function activate($base = null) { 00101 if ($this->__active === true) { 00102 return; 00103 } 00104 parent::__construct($base); 00105 $this->__active = true; 00106 } 00107 /** 00108 * Used to write a value to a session key. 00109 * 00110 * In your controller: $this->Session->write('Controller.sessKey', 'session value'); 00111 * 00112 * @param string $name The name of the key your are setting in the session. 00113 * This should be in a Controller.key format for better organizing 00114 * @param string $value The value you want to store in a session. 00115 * @access public 00116 */ 00117 function write($name, $value = null) { 00118 if ($this->__active === true) { 00119 $this->__start(); 00120 if (is_array($name)) { 00121 foreach ($name as $key => $value) { 00122 if (parent::write($key, $value) === false) { 00123 return false; 00124 } 00125 } 00126 return true; 00127 } 00128 if (parent::write($name, $value) === false) { 00129 return false; 00130 } 00131 return true; 00132 } 00133 return false; 00134 } 00135 /** 00136 * Used to read a session values for a key or return values for all keys. 00137 * 00138 * In your controller: $this->Session->read('Controller.sessKey'); 00139 * Calling the method without a param will return all session vars 00140 * 00141 * @param string $name the name of the session key you want to read 00142 * @return mixed value from the session vars 00143 * @access public 00144 */ 00145 function read($name = null) { 00146 if ($this->__active === true) { 00147 $this->__start(); 00148 return parent::read($name); 00149 } 00150 return false; 00151 } 00152 /** 00153 * Used to delete a session variable. 00154 * 00155 * In your controller: $this->Session->del('Controller.sessKey'); 00156 * 00157 * @param string $name the name of the session key you want to delete 00158 * @return boolean true is session variable is set and can be deleted, false is variable was not set. 00159 * @access public 00160 */ 00161 function del($name) { 00162 if ($this->__active === true) { 00163 $this->__start(); 00164 return parent::del($name); 00165 } 00166 return false; 00167 } 00168 /** 00169 * Wrapper for SessionComponent::del(); 00170 * 00171 * In your controller: $this->Session->delete('Controller.sessKey'); 00172 * 00173 * @param string $name the name of the session key you want to delete 00174 * @return boolean true is session variable is set and can be deleted, false is variable was not set. 00175 * @access public 00176 */ 00177 function delete($name) { 00178 if ($this->__active === true) { 00179 $this->__start(); 00180 return $this->del($name); 00181 } 00182 return false; 00183 } 00184 /** 00185 * Used to check if a session variable is set 00186 * 00187 * In your controller: $this->Session->check('Controller.sessKey'); 00188 * 00189 * @param string $name the name of the session key you want to check 00190 * @return boolean true is session variable is set, false if not 00191 * @access public 00192 */ 00193 function check($name) { 00194 if ($this->__active === true) { 00195 $this->__start(); 00196 return parent::check($name); 00197 } 00198 return false; 00199 } 00200 /** 00201 * Used to determine the last error in a session. 00202 * 00203 * In your controller: $this->Session->error(); 00204 * 00205 * @return string Last session error 00206 * @access public 00207 */ 00208 function error() { 00209 if ($this->__active === true) { 00210 $this->__start(); 00211 return parent::error(); 00212 } 00213 return false; 00214 } 00215 /** 00216 * Used to set a session variable that can be used to output messages in the view. 00217 * 00218 * In your controller: $this->Session->setFlash('This has been saved'); 00219 * 00220 * Additional params below can be passed to customize the output, or the Message.[key] 00221 * 00222 * @param string $message Message to be flashed 00223 * @param string $layout Layout to wrap flash message in 00224 * @param array $params Parameters to be sent to layout as view variables 00225 * @param string $key Message key, default is 'flash' 00226 * @access public 00227 */ 00228 function setFlash($message, $layout = 'default', $params = array(), $key = 'flash') { 00229 if ($this->__active === true) { 00230 $this->__start(); 00231 $this->write('Message.' . $key, compact('message', 'layout', 'params')); 00232 } 00233 } 00234 /** 00235 * Used to renew a session id 00236 * 00237 * In your controller: $this->Session->renew(); 00238 * 00239 * @access public 00240 */ 00241 function renew() { 00242 if ($this->__active === true) { 00243 $this->__start(); 00244 parent::renew(); 00245 } 00246 } 00247 /** 00248 * Used to check for a valid session. 00249 * 00250 * In your controller: $this->Session->valid(); 00251 * 00252 * @return boolean true is session is valid, false is session is invalid 00253 * @access public 00254 */ 00255 function valid() { 00256 if ($this->__active === true) { 00257 $this->__start(); 00258 return parent::valid(); 00259 } 00260 return false; 00261 } 00262 /** 00263 * Used to destroy sessions 00264 * 00265 * In your controller: $this->Session->destroy(); 00266 * 00267 * @access public 00268 */ 00269 function destroy() { 00270 if ($this->__active === true) { 00271 $this->__start(); 00272 parent::destroy(); 00273 } 00274 } 00275 /** 00276 * Returns Session id 00277 * 00278 * If $id is passed in a beforeFilter, the Session will be started 00279 * with the specified id 00280 * 00281 * @param $id string 00282 * @return string 00283 * @access public 00284 */ 00285 function id($id = null) { 00286 return parent::id($id); 00287 } 00288 /** 00289 * Starts Session if SessionComponent is used in Controller::beforeFilter(), 00290 * or is called from 00291 * 00292 * @access private 00293 */ 00294 function __start(){ 00295 if ($this->__started === false) { 00296 if (!$this->id() && parent::start()) { 00297 $this->__started = true; 00298 parent::_checkValid(); 00299 } else { 00300 $this->__started = parent::start(); 00301 } 00302 } 00303 return $this->__started; 00304 } 00305 } 00306 ?>