You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					679 lines
				
				19 KiB
			
		
		
			
		
	
	
					679 lines
				
				19 KiB
			| 
											14 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
| 
											13 years ago
										 |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
| 
											14 years ago
										 |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											13 years ago
										 | namespace yii\web;
 | ||
|  | 
 | ||
| 
											13 years ago
										 | use Yii;
 | ||
| 
											13 years ago
										 | use yii\base\Component;
 | ||
|  | use yii\base\InvalidParamException;
 | ||
|  | 
 | ||
| 
											14 years ago
										 | /**
 | ||
| 
											13 years ago
										 |  * Session provides session data management and the related configurations.
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											13 years ago
										 |  * Session is a Web application component that can be accessed via `Yii::$app->session`.
 | ||
|  | 
 | ||
| 
											13 years ago
										 |  * To start the session, call [[open()]]; To complete and send out session data, call [[close()]];
 | ||
|  |  * To destroy the session, call [[destroy()]].
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											13 years ago
										 |  * By default, [[autoStart]] is true which means the session will be started automatically
 | ||
|  |  * when the session component is accessed the first time.
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											13 years ago
										 |  * Session can be used like an array to set and get session data. For example,
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											13 years ago
										 |  * ~~~
 | ||
|  |  * $session = new Session;
 | ||
|  |  * $session->open();
 | ||
|  |  * $value1 = $session['name1'];  // get session variable 'name1'
 | ||
|  |  * $value2 = $session['name2'];  // get session variable 'name2'
 | ||
|  |  * foreach ($session as $name => $value) // traverse all session variables
 | ||
|  |  * $session['name3'] = $value3;  // set session variable 'name3'
 | ||
|  |  * ~~~
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											13 years ago
										 |  * Session can be extended to support customized session storage.
 | ||
| 
											13 years ago
										 |  * To do so, override [[useCustomStorage()]] so that it returns true, and
 | ||
|  |  * override these methods with the actual logic about using custom storage:
 | ||
|  |  * [[openSession()]], [[closeSession()]], [[readSession()]], [[writeSession()]],
 | ||
|  |  * [[destroySession()]] and [[gcSession()]].
 | ||
| 
											14 years ago
										 |  *
 | ||
| 
											13 years ago
										 |  * Session also supports a special type of session data, called *flash messages*.
 | ||
|  |  * A flash message is available only in the current request and the next request.
 | ||
|  |  * After that, it will be deleted automatically. Flash messages are particularly
 | ||
|  |  * useful for displaying confirmation messages. To use flash messages, simply
 | ||
|  |  * call methods such as [[setFlash()]], [[getFlash()]].
 | ||
| 
											14 years ago
										 |  *
 | ||
|  |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
| 
											13 years ago
										 |  * @since 2.0
 | ||
| 
											14 years ago
										 |  */
 | ||
| 
											13 years ago
										 | class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Countable
 | ||
| 
											14 years ago
										 | {
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var boolean whether the session should be automatically started when the session component is initialized.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public $autoStart = true;
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @var string the name of the session variable that stores the flash message data.
 | ||
|  | 	 */
 | ||
|  | 	public $flashVar = '__flash';
 | ||
| 
											14 years ago
										 | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @var array parameter-value pairs to override default session cookie parameters
 | ||
|  | 	 */
 | ||
|  | 	public $cookieParams = array(
 | ||
|  | 		'httponly' => true
 | ||
|  | 	);
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * Initializes the application component.
 | ||
|  | 	 * This method is required by IApplicationComponent and is invoked by application.
 | ||
|  | 	 */
 | ||
|  | 	public function init()
 | ||
|  | 	{
 | ||
|  | 		parent::init();
 | ||
| 
											13 years ago
										 | 		if ($this->autoStart) {
 | ||
| 
											14 years ago
										 | 			$this->open();
 | ||
| 
											13 years ago
										 | 		}
 | ||
|  | 		register_shutdown_function(array($this, 'close'));
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns a value indicating whether to use custom session storage.
 | ||
| 
											13 years ago
										 | 	 * This method should be overridden to return true by child classes that implement custom session storage.
 | ||
|  | 	 * To implement custom session storage, override these methods: [[openSession()]], [[closeSession()]],
 | ||
|  | 	 * [[readSession()]], [[writeSession()]], [[destroySession()]] and [[gcSession()]].
 | ||
| 
											14 years ago
										 | 	 * @return boolean whether to use custom storage.
 | ||
|  | 	 */
 | ||
|  | 	public function getUseCustomStorage()
 | ||
|  | 	{
 | ||
|  | 		return false;
 | ||
|  | 	}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	private $_opened = false;
 | ||
|  | 
 | ||
| 
											14 years ago
										 | 	/**
 | ||
| 
											13 years ago
										 | 	 * Starts the session.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public function open()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		// this is available in PHP 5.4.0+
 | ||
|  | 		if (function_exists('session_status')) {
 | ||
|  | 			if (session_status() == PHP_SESSION_ACTIVE) {
 | ||
| 
											13 years ago
										 | 				$this->_opened = true;
 | ||
| 
											13 years ago
										 | 				return;
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 		if (!$this->_opened) {
 | ||
|  | 			if ($this->getUseCustomStorage()) {
 | ||
|  | 				@session_set_save_handler(
 | ||
|  | 					array($this, 'openSession'),
 | ||
|  | 					array($this, 'closeSession'),
 | ||
|  | 					array($this, 'readSession'),
 | ||
|  | 					array($this, 'writeSession'),
 | ||
|  | 					array($this, 'destroySession'),
 | ||
|  | 					array($this, 'gcSession')
 | ||
|  | 				);
 | ||
|  | 			}
 | ||
| 
											14 years ago
										 | 
 | ||
| 
											13 years ago
										 | 			$this->setCookieParams($this->cookieParams);
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 			@session_start();
 | ||
| 
											13 years ago
										 | 
 | ||
| 
											13 years ago
										 | 			if (session_id() == '') {
 | ||
|  | 				$this->_opened = false;
 | ||
|  | 				$error = error_get_last();
 | ||
|  | 				$message = isset($error['message']) ? $error['message'] : 'Failed to start session.';
 | ||
| 
											13 years ago
										 | 				Yii::error($message, __METHOD__);
 | ||
| 
											13 years ago
										 | 			} else {
 | ||
|  | 				$this->_opened = true;
 | ||
|  | 				$this->updateFlashCounters();
 | ||
|  | 			}
 | ||
| 
											14 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Ends the current session and store session data.
 | ||
|  | 	 */
 | ||
|  | 	public function close()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$this->_opened = false;
 | ||
| 
											13 years ago
										 | 		if (session_id() !== '') {
 | ||
| 
											14 years ago
										 | 			@session_write_close();
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Frees all session variables and destroys all data registered to a session.
 | ||
|  | 	 */
 | ||
|  | 	public function destroy()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		if (session_id() !== '') {
 | ||
| 
											14 years ago
										 | 			@session_unset();
 | ||
|  | 			@session_destroy();
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return boolean whether the session has started
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function getIsActive()
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if (function_exists('session_status')) {
 | ||
|  | 			// available in PHP 5.4.0+
 | ||
|  | 			return session_status() == PHP_SESSION_ACTIVE;
 | ||
|  | 		} else {
 | ||
|  | 			// this is not very reliable
 | ||
| 
											13 years ago
										 | 			return $this->_opened && session_id() !== '';
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return string the current session ID
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function getId()
 | ||
| 
											14 years ago
										 | 	{
 | ||
|  | 		return session_id();
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @param string $value the session ID for the current session
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function setId($value)
 | ||
| 
											14 years ago
										 | 	{
 | ||
|  | 		session_id($value);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Updates the current session ID with a newly generated one .
 | ||
|  | 	 * Please refer to [[http://php.net/session_regenerate_id]] for more details.
 | ||
| 
											14 years ago
										 | 	 * @param boolean $deleteOldSession Whether to delete the old associated session file or not.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function regenerateID($deleteOldSession = false)
 | ||
| 
											14 years ago
										 | 	{
 | ||
|  | 		session_regenerate_id($deleteOldSession);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return string the current session name
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function getName()
 | ||
| 
											14 years ago
										 | 	{
 | ||
|  | 		return session_name();
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @param string $value the session name for the current session, must be an alphanumeric string.
 | ||
|  | 	 * It defaults to "PHPSESSID".
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function setName($value)
 | ||
| 
											14 years ago
										 | 	{
 | ||
|  | 		session_name($value);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return string the current session save path, defaults to '/tmp'.
 | ||
|  | 	 */
 | ||
|  | 	public function getSavePath()
 | ||
|  | 	{
 | ||
|  | 		return session_save_path();
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @param string $value the current session save path. This can be either a directory name or a path alias.
 | ||
|  | 	 * @throws InvalidParamException if the path is not a valid directory
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public function setSavePath($value)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$path = Yii::getAlias($value);
 | ||
| 
											13 years ago
										 | 		if (is_dir($path)) {
 | ||
| 
											13 years ago
										 | 			session_save_path($path);
 | ||
| 
											13 years ago
										 | 		} else {
 | ||
| 
											13 years ago
										 | 			throw new InvalidParamException("Session save path is not a valid directory: $value");
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return array the session cookie parameters.
 | ||
|  | 	 * @see http://us2.php.net/manual/en/function.session-get-cookie-params.php
 | ||
|  | 	 */
 | ||
|  | 	public function getCookieParams()
 | ||
|  | 	{
 | ||
|  | 		return session_get_cookie_params();
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Sets the session cookie parameters.
 | ||
|  | 	 * The effect of this method only lasts for the duration of the script.
 | ||
|  | 	 * Call this method before the session starts.
 | ||
| 
											13 years ago
										 | 	 * @param array $value cookie parameters, valid keys include: lifetime, path, domain, secure and httponly.
 | ||
|  | 	 * @throws InvalidParamException if the parameters are incomplete.
 | ||
| 
											14 years ago
										 | 	 * @see http://us2.php.net/manual/en/function.session-set-cookie-params.php
 | ||
|  | 	 */
 | ||
|  | 	public function setCookieParams($value)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$data = session_get_cookie_params();
 | ||
| 
											14 years ago
										 | 		extract($data);
 | ||
|  | 		extract($value);
 | ||
| 
											13 years ago
										 | 		if (isset($lifetime, $path, $domain, $secure, $httponly)) {
 | ||
| 
											13 years ago
										 | 			session_set_cookie_params($lifetime, $path, $domain, $secure, $httponly);
 | ||
|  | 		} else {
 | ||
| 
											13 years ago
										 | 			throw new InvalidParamException('Please make sure these parameters are provided: lifetime, path, domain, secure and httponly.');
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Returns the value indicating whether cookies should be used to store session IDs.
 | ||
|  | 	 * @return boolean|null the value indicating whether cookies should be used to store session IDs.
 | ||
|  | 	 * @see setUseCookies()
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function getUseCookies()
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if (ini_get('session.use_cookies') === '0') {
 | ||
| 
											13 years ago
										 | 			return false;
 | ||
|  | 		} elseif (ini_get('session.use_only_cookies') === '1') {
 | ||
|  | 			return true;
 | ||
| 
											13 years ago
										 | 		} else {
 | ||
| 
											13 years ago
										 | 			return null;
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Sets the value indicating whether cookies should be used to store session IDs.
 | ||
|  | 	 * Three states are possible:
 | ||
|  | 	 *
 | ||
|  | 	 * - true: cookies and only cookies will be used to store session IDs.
 | ||
|  | 	 * - false: cookies will not be used to store session IDs.
 | ||
|  | 	 * - null: if possible, cookies will be used to store session IDs; if not, other mechanisms will be used (e.g. GET parameter)
 | ||
|  | 	 *
 | ||
|  | 	 * @param boolean|null $value the value indicating whether cookies should be used to store session IDs.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
| 
											13 years ago
										 | 	public function setUseCookies($value)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		if ($value === false) {
 | ||
| 
											13 years ago
										 | 			ini_set('session.use_cookies', '0');
 | ||
|  | 			ini_set('session.use_only_cookies', '0');
 | ||
| 
											13 years ago
										 | 		} elseif ($value === true) {
 | ||
|  | 			ini_set('session.use_cookies', '1');
 | ||
|  | 			ini_set('session.use_only_cookies', '1');
 | ||
| 
											13 years ago
										 | 		} else {
 | ||
| 
											13 years ago
										 | 			ini_set('session.use_cookies', '1');
 | ||
|  | 			ini_set('session.use_only_cookies', '0');
 | ||
| 
											14 years ago
										 | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @return float the probability (percentage) that the GC (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public function getGCProbability()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return (float)(ini_get('session.gc_probability') / ini_get('session.gc_divisor') * 100);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @param float $value the probability (percentage) that the GC (garbage collection) process is started on every session initialization.
 | ||
|  | 	 * @throws InvalidParamException if the value is not between 0 and 100.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public function setGCProbability($value)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		if ($value >= 0 && $value <= 100) {
 | ||
| 
											13 years ago
										 | 			// percent * 21474837 / 2147483647 ≈ percent * 0.01
 | ||
|  | 			ini_set('session.gc_probability', floor($value * 21474836.47));
 | ||
|  | 			ini_set('session.gc_divisor', 2147483647);
 | ||
| 
											13 years ago
										 | 		} else {
 | ||
| 
											13 years ago
										 | 			throw new InvalidParamException('GCProbability must be a value between 0 and 100.');
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return boolean whether transparent sid support is enabled or not, defaults to false.
 | ||
|  | 	 */
 | ||
|  | 	public function getUseTransparentSessionID()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return ini_get('session.use_trans_sid') == 1;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @param boolean $value whether transparent sid support is enabled or not.
 | ||
|  | 	 */
 | ||
|  | 	public function setUseTransparentSessionID($value)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		ini_set('session.use_trans_sid', $value ? '1' : '0');
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * @return integer the number of seconds after which data will be seen as 'garbage' and cleaned up.
 | ||
|  | 	 * The default value is 1440 seconds (or the value of "session.gc_maxlifetime" set in php.ini).
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public function getTimeout()
 | ||
|  | 	{
 | ||
|  | 		return (int)ini_get('session.gc_maxlifetime');
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @param integer $value the number of seconds after which data will be seen as 'garbage' and cleaned up
 | ||
|  | 	 */
 | ||
|  | 	public function setTimeout($value)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		ini_set('session.gc_maxlifetime', $value);
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Session open handler.
 | ||
| 
											13 years ago
										 | 	 * This method should be overridden if [[useCustomStorage()]] returns true.
 | ||
| 
											14 years ago
										 | 	 * Do not call this method directly.
 | ||
|  | 	 * @param string $savePath session save path
 | ||
|  | 	 * @param string $sessionName session name
 | ||
|  | 	 * @return boolean whether session is opened successfully
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function openSession($savePath, $sessionName)
 | ||
| 
											14 years ago
										 | 	{
 | ||
|  | 		return true;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Session close handler.
 | ||
| 
											13 years ago
										 | 	 * This method should be overridden if [[useCustomStorage()]] returns true.
 | ||
| 
											14 years ago
										 | 	 * Do not call this method directly.
 | ||
|  | 	 * @return boolean whether session is closed successfully
 | ||
|  | 	 */
 | ||
|  | 	public function closeSession()
 | ||
|  | 	{
 | ||
|  | 		return true;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Session read handler.
 | ||
| 
											13 years ago
										 | 	 * This method should be overridden if [[useCustomStorage()]] returns true.
 | ||
| 
											14 years ago
										 | 	 * Do not call this method directly.
 | ||
|  | 	 * @param string $id session ID
 | ||
|  | 	 * @return string the session data
 | ||
|  | 	 */
 | ||
|  | 	public function readSession($id)
 | ||
|  | 	{
 | ||
|  | 		return '';
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Session write handler.
 | ||
| 
											13 years ago
										 | 	 * This method should be overridden if [[useCustomStorage()]] returns true.
 | ||
| 
											14 years ago
										 | 	 * Do not call this method directly.
 | ||
|  | 	 * @param string $id session ID
 | ||
|  | 	 * @param string $data session data
 | ||
|  | 	 * @return boolean whether session write is successful
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function writeSession($id, $data)
 | ||
| 
											14 years ago
										 | 	{
 | ||
|  | 		return true;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Session destroy handler.
 | ||
| 
											13 years ago
										 | 	 * This method should be overridden if [[useCustomStorage()]] returns true.
 | ||
| 
											14 years ago
										 | 	 * Do not call this method directly.
 | ||
|  | 	 * @param string $id session ID
 | ||
|  | 	 * @return boolean whether session is destroyed successfully
 | ||
|  | 	 */
 | ||
|  | 	public function destroySession($id)
 | ||
|  | 	{
 | ||
|  | 		return true;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Session GC (garbage collection) handler.
 | ||
| 
											13 years ago
										 | 	 * This method should be overridden if [[useCustomStorage()]] returns true.
 | ||
| 
											14 years ago
										 | 	 * Do not call this method directly.
 | ||
|  | 	 * @param integer $maxLifetime the number of seconds after which data will be seen as 'garbage' and cleaned up.
 | ||
|  | 	 * @return boolean whether session is GCed successfully
 | ||
|  | 	 */
 | ||
|  | 	public function gcSession($maxLifetime)
 | ||
|  | 	{
 | ||
|  | 		return true;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns an iterator for traversing the session variables.
 | ||
|  | 	 * This method is required by the interface IteratorAggregate.
 | ||
| 
											13 years ago
										 | 	 * @return SessionIterator an iterator for traversing the session variables.
 | ||
| 
											14 years ago
										 | 	 */
 | ||
|  | 	public function getIterator()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return new SessionIterator;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns the number of items in the session.
 | ||
|  | 	 * @return integer the number of session variables
 | ||
|  | 	 */
 | ||
|  | 	public function getCount()
 | ||
|  | 	{
 | ||
|  | 		return count($_SESSION);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns the number of items in the session.
 | ||
|  | 	 * This method is required by Countable interface.
 | ||
|  | 	 * @return integer number of items in the session.
 | ||
|  | 	 */
 | ||
|  | 	public function count()
 | ||
|  | 	{
 | ||
|  | 		return $this->getCount();
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns the session variable value with the session variable name.
 | ||
| 
											13 years ago
										 | 	 * If the session variable does not exist, the `$defaultValue` will be returned.
 | ||
|  | 	 * @param string $key the session variable name
 | ||
| 
											14 years ago
										 | 	 * @param mixed $defaultValue the default value to be returned when the session variable does not exist.
 | ||
|  | 	 * @return mixed the session variable value, or $defaultValue if the session variable does not exist.
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function get($key, $defaultValue = null)
 | ||
| 
											14 years ago
										 | 	{
 | ||
|  | 		return isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Adds a session variable.
 | ||
| 
											13 years ago
										 | 	 * If the specified name already exists, the old value will be overwritten.
 | ||
|  | 	 * @param string $key session variable name
 | ||
| 
											14 years ago
										 | 	 * @param mixed $value session variable value
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function set($key, $value)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$_SESSION[$key] = $value;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Removes a session variable.
 | ||
| 
											13 years ago
										 | 	 * @param string $key the name of the session variable to be removed
 | ||
| 
											14 years ago
										 | 	 * @return mixed the removed value, null if no such session variable.
 | ||
|  | 	 */
 | ||
|  | 	public function remove($key)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		if (isset($_SESSION[$key])) {
 | ||
|  | 			$value = $_SESSION[$key];
 | ||
| 
											14 years ago
										 | 			unset($_SESSION[$key]);
 | ||
|  | 			return $value;
 | ||
| 
											13 years ago
										 | 		} else {
 | ||
| 
											14 years ago
										 | 			return null;
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Removes all session variables
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function removeAll()
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		foreach (array_keys($_SESSION) as $key) {
 | ||
| 
											14 years ago
										 | 			unset($_SESSION[$key]);
 | ||
| 
											13 years ago
										 | 		}
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @param mixed $key session variable name
 | ||
|  | 	 * @return boolean whether there is the named session variable
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function has($key)
 | ||
| 
											14 years ago
										 | 	{
 | ||
|  | 		return isset($_SESSION[$key]);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return array the list of all session variables in array
 | ||
|  | 	 */
 | ||
|  | 	public function toArray()
 | ||
|  | 	{
 | ||
|  | 		return $_SESSION;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Updates the counters for flash messages and removes outdated flash messages.
 | ||
|  | 	 * This method should only be called once in [[init()]].
 | ||
|  | 	 */
 | ||
|  | 	protected function updateFlashCounters()
 | ||
|  | 	{
 | ||
|  | 		$counters = $this->get($this->flashVar, array());
 | ||
|  | 		if (is_array($counters)) {
 | ||
|  | 			foreach ($counters as $key => $count) {
 | ||
|  | 				if ($count) {
 | ||
|  | 					unset($counters[$key], $_SESSION[$key]);
 | ||
|  | 				} else {
 | ||
|  | 					$counters[$key]++;
 | ||
|  | 				}
 | ||
|  | 			}
 | ||
|  | 			$_SESSION[$this->flashVar] = $counters;
 | ||
|  | 		} else {
 | ||
|  | 			// fix the unexpected problem that flashVar doesn't return an array
 | ||
|  | 			unset($_SESSION[$this->flashVar]);
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns a flash message.
 | ||
|  | 	 * A flash message is available only in the current request and the next request.
 | ||
|  | 	 * @param string $key the key identifying the flash message
 | ||
|  | 	 * @param mixed $defaultValue value to be returned if the flash message does not exist.
 | ||
|  | 	 * @return mixed the flash message
 | ||
|  | 	 */
 | ||
|  | 	public function getFlash($key, $defaultValue = null)
 | ||
|  | 	{
 | ||
|  | 		$counters = $this->get($this->flashVar, array());
 | ||
|  | 		return isset($counters[$key]) ? $this->get($key, $defaultValue) : $defaultValue;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns all flash messages.
 | ||
|  | 	 * @return array flash messages (key => message).
 | ||
|  | 	 */
 | ||
|  | 	public function getAllFlashes()
 | ||
|  | 	{
 | ||
|  | 		$counters = $this->get($this->flashVar, array());
 | ||
|  | 		$flashes = array();
 | ||
|  | 		foreach (array_keys($counters) as $key) {
 | ||
|  | 			if (isset($_SESSION[$key])) {
 | ||
|  | 				$flashes[$key] = $_SESSION[$key];
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 		return $flashes;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Stores a flash message.
 | ||
|  | 	 * A flash message is available only in the current request and the next request.
 | ||
|  | 	 * @param string $key the key identifying the flash message. Note that flash messages
 | ||
|  | 	 * and normal session variables share the same name space. If you have a normal
 | ||
|  | 	 * session variable using the same name, its value will be overwritten by this method.
 | ||
|  | 	 * @param mixed $value flash message
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function setFlash($key, $value = true)
 | ||
| 
											13 years ago
										 | 	{
 | ||
|  | 		$counters = $this->get($this->flashVar, array());
 | ||
|  | 		$counters[$key] = 0;
 | ||
|  | 		$_SESSION[$key] = $value;
 | ||
|  | 		$_SESSION[$this->flashVar] = $counters;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Removes a flash message.
 | ||
|  | 	 * Note that flash messages will be automatically removed after the next request.
 | ||
|  | 	 * @param string $key the key identifying the flash message. Note that flash messages
 | ||
|  | 	 * and normal session variables share the same name space.  If you have a normal
 | ||
|  | 	 * session variable using the same name, it will be removed by this method.
 | ||
|  | 	 * @return mixed the removed flash message. Null if the flash message does not exist.
 | ||
|  | 	 */
 | ||
|  | 	public function removeFlash($key)
 | ||
|  | 	{
 | ||
|  | 		$counters = $this->get($this->flashVar, array());
 | ||
|  | 		$value = isset($_SESSION[$key], $counters[$key]) ? $_SESSION[$key] : null;
 | ||
|  | 		unset($counters[$key], $_SESSION[$key]);
 | ||
|  | 		$_SESSION[$this->flashVar] = $counters;
 | ||
|  | 		return $value;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Removes all flash messages.
 | ||
|  | 	 * Note that flash messages and normal session variables share the same name space.
 | ||
|  | 	 * If you have a normal session variable using the same name, it will be removed
 | ||
|  | 	 * by this method.
 | ||
|  | 	 */
 | ||
|  | 	public function removeAllFlashes()
 | ||
|  | 	{
 | ||
|  | 		$counters = $this->get($this->flashVar, array());
 | ||
|  | 		foreach (array_keys($counters) as $key) {
 | ||
|  | 			unset($_SESSION[$key]);
 | ||
|  | 		}
 | ||
|  | 		unset($_SESSION[$this->flashVar]);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											13 years ago
										 | 	 * Returns a value indicating whether there is a flash message associated with the specified key.
 | ||
| 
											13 years ago
										 | 	 * @param string $key key identifying the flash message
 | ||
|  | 	 * @return boolean whether the specified flash message exists
 | ||
|  | 	 */
 | ||
|  | 	public function hasFlash($key)
 | ||
|  | 	{
 | ||
|  | 		return $this->getFlash($key) !== null;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
| 
											14 years ago
										 | 	 * This method is required by the interface ArrayAccess.
 | ||
|  | 	 * @param mixed $offset the offset to check on
 | ||
|  | 	 * @return boolean
 | ||
|  | 	 */
 | ||
|  | 	public function offsetExists($offset)
 | ||
|  | 	{
 | ||
|  | 		return isset($_SESSION[$offset]);
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * This method is required by the interface ArrayAccess.
 | ||
|  | 	 * @param integer $offset the offset to retrieve element.
 | ||
|  | 	 * @return mixed the element at the offset, null if no element is found at the offset
 | ||
|  | 	 */
 | ||
|  | 	public function offsetGet($offset)
 | ||
|  | 	{
 | ||
|  | 		return isset($_SESSION[$offset]) ? $_SESSION[$offset] : null;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * This method is required by the interface ArrayAccess.
 | ||
|  | 	 * @param integer $offset the offset to set element
 | ||
|  | 	 * @param mixed $item the element value
 | ||
|  | 	 */
 | ||
| 
											13 years ago
										 | 	public function offsetSet($offset, $item)
 | ||
| 
											14 years ago
										 | 	{
 | ||
| 
											13 years ago
										 | 		$_SESSION[$offset] = $item;
 | ||
| 
											14 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * This method is required by the interface ArrayAccess.
 | ||
|  | 	 * @param mixed $offset the offset to unset element
 | ||
|  | 	 */
 | ||
|  | 	public function offsetUnset($offset)
 | ||
|  | 	{
 | ||
|  | 		unset($_SESSION[$offset]);
 | ||
|  | 	}
 | ||
|  | }
 |