📄 session.php
字号:
throw new Zend_Session_Exception('session has already been started by session.auto-start or session_start()'); } /** * Hack to throw exceptions on start instead of php errors * @see http://framework.zend.com/issues/browse/ZF-1325 */ /** @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; set_error_handler(array('Zend_Session_Exception', 'handleSessionStartError'), E_ALL); session_start(); restore_error_handler(); if (Zend_Session_Exception::$sessionStartError !== null) { set_error_handler(array('Zend_Session_Exception', 'handleSilentWriteClose'), E_ALL); session_write_close(); restore_error_handler(); throw new Zend_Session_Exception(__CLASS__ . '::' . __FUNCTION__ . '() - ' . Zend_Session_Exception::$sessionStartError); } parent::$_readable = true; parent::$_writable = true; self::$_sessionStarted = true; if (self::$_regenerateIdState === -1) { self::regenerateId(); } // run validators if they exist if (isset($_SESSION['__ZF']['VALID'])) { self::_processValidators(); } self::_processStartupMetadataGlobal(); } /** * _processGlobalMetadata() - this method initizes the sessions GLOBAL * metadata, mostly global data expiration calculations. * * @return void */ private static function _processStartupMetadataGlobal() { // process global metadata if (isset($_SESSION['__ZF'])) { // expire globally expired values foreach ($_SESSION['__ZF'] as $namespace => $namespace_metadata) { // Expire Namespace by Time (ENT) if (isset($namespace_metadata['ENT']) && ($namespace_metadata['ENT'] > 0) && (time() > $namespace_metadata['ENT']) ) { unset($_SESSION[$namespace]); unset($_SESSION['__ZF'][$namespace]['ENT']); } // Expire Namespace by Global Hop (ENGH) if (isset($namespace_metadata['ENGH']) && $namespace_metadata['ENGH'] >= 1) { $_SESSION['__ZF'][$namespace]['ENGH']--; if ($_SESSION['__ZF'][$namespace]['ENGH'] === 0) { if (isset($_SESSION[$namespace])) { parent::$_expiringData[$namespace] = $_SESSION[$namespace]; unset($_SESSION[$namespace]); } unset($_SESSION['__ZF'][$namespace]['ENGH']); } } // Expire Namespace Variables by Time (ENVT) if (isset($namespace_metadata['ENVT'])) { foreach ($namespace_metadata['ENVT'] as $variable => $time) { if (time() > $time) { unset($_SESSION[$namespace][$variable]); unset($_SESSION['__ZF'][$namespace]['ENVT'][$variable]); if (empty($_SESSION['__ZF'][$namespace]['ENVT'])) { unset($_SESSION['__ZF'][$namespace]['ENVT']); } } } } // Expire Namespace Variables by Global Hop (ENVGH) if (isset($namespace_metadata['ENVGH'])) { foreach ($namespace_metadata['ENVGH'] as $variable => $hops) { $_SESSION['__ZF'][$namespace]['ENVGH'][$variable]--; if ($_SESSION['__ZF'][$namespace]['ENVGH'][$variable] === 0) { if (isset($_SESSION[$namespace][$variable])) { parent::$_expiringData[$namespace][$variable] = $_SESSION[$namespace][$variable]; unset($_SESSION[$namespace][$variable]); } unset($_SESSION['__ZF'][$namespace]['ENVGH'][$variable]); } } } } if (empty($_SESSION['__ZF'][$namespace])) { unset($_SESSION['__ZF'][$namespace]); } } if (empty($_SESSION['__ZF'])) { unset($_SESSION['__ZF']); } } /** * isStarted() - convenience method to determine if the session is already started. * * @return bool */ public static function isStarted() { return self::$_sessionStarted; } /** * isRegenerated() - convenience method to determine if session_regenerate_id() * has been called during this request by Zend_Session. * * @return bool */ public static function isRegenerated() { return ( (self::$_regenerateIdState > 0) ? true : false ); } /** * getId() - get the current session id * * @return string */ public static function getId() { return session_id(); } /** * setId() - set an id to a user specified id * * @throws Zend_Session_Exception * @param string $id * @return void */ public static function setId($id) { if (defined('SID')) { /** @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception('The session has already been started. The session id must be set first.'); } if (headers_sent($filename, $linenum)) { /** @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception("You must call ".__CLASS__.'::'.__FUNCTION__. "() before any output has been sent to the browser; output started in {$filename}/{$linenum}"); } if (!is_string($id) || $id === '') { /** @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception('You must provide a non-empty string as a session identifier.'); } session_id($id); } /** * registerValidator() - register a validator that will attempt to validate this session for * every future request * * @param Zend_Session_Validator_Interface $validator * @return void */ public static function registerValidator(Zend_Session_Validator_Interface $validator) { $validator->setup(); } /** * stop() - Disable write access. Optionally disable read (not implemented). * * @return void */ public static function stop() { parent::$_writable = false; } /** * writeClose() - Shutdown the sesssion, close writing and detach $_SESSION from the back-end storage mechanism. * This will complete the internal data transformation on this request. * * @param bool $readonly - OPTIONAL remove write access (i.e. throw error if Zend_Session's attempt writes) * @return void */ public static function writeClose($readonly = true) { if (self::$_writeClosed) { return; } if ($readonly) { parent::$_writable = false; } session_write_close(); self::$_writeClosed = true; } /** * destroy() - This is used to destroy session data, and optionally, the session cookie itself * * @param bool $remove_cookie - OPTIONAL remove session id cookie, defaults to true (remove cookie) * @param bool $readonly - OPTIONAL remove write access (i.e. throw error if Zend_Session's attempt writes) * @return void */ public static function destroy($remove_cookie = true, $readonly = true) { if (self::$_destroyed) { return; } if ($readonly) { parent::$_writable = false; } session_destroy(); self::$_destroyed = true; if ($remove_cookie) { self::expireSessionCookie(); } } /** * expireSessionCookie() - Sends an expired session id cookie, causing the client to delete the session cookie * * @return void */ public static function expireSessionCookie() { if (self::$_sessionCookieDeleted) { return; } self::$_sessionCookieDeleted = true; if (isset($_COOKIE[session_name()])) { $cookie_params = session_get_cookie_params(); setcookie( session_name(), false, 315554400, // strtotime('1980-01-01'), $cookie_params['path'], $cookie_params['domain'], $cookie_params['secure'] ); } } /** * _processValidator() - internal function that is called in the existence of VALID metadata * * @throws Zend_Session_Exception * @return void */ private static function _processValidators() { if (count($_SESSION['__ZF']['VALID']) > 0) { /** * @see Zend_Loader */ require_once 'Zend/Loader.php'; } foreach ($_SESSION['__ZF']['VALID'] as $validator_name => $valid_data) { Zend_Loader::loadClass($validator_name); $validator = new $validator_name; if ($validator->validate() === false) { /** @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception("This session is not valid according to {$validator_name}."); } } } /** * namespaceIsset() - check to see if a namespace is set * * @param string $namespace * @return bool */ public static function namespaceIsset($namespace) { return parent::_namespaceIsset($namespace); } /** * namespaceUnset() - unset a namespace or a variable within a namespace * * @param string $namespace * @throws Zend_Session_Exception * @return void */ public static function namespaceUnset($namespace) { parent::_namespaceUnset($namespace); } /** * namespaceGet() - get all variables in a namespace * Deprecated: Use getIterator() in Zend_Session_Namespace. * * @param string $namespace * @return array */ public static function namespaceGet($namespace) { return parent::_namespaceGetAll($namespace); } /** * getIterator() - return an iteratable object for use in foreach and the like, * this completes the IteratorAggregate interface * * @throws Zend_Session_Exception * @return ArrayObject */ public static function getIterator() { if (parent::$_readable === false) { /** @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception(parent::_THROW_NOT_READABLE_MSG); } $spaces = array(); if (isset($_SESSION)) { $spaces = array_keys($_SESSION); foreach($spaces as $key => $space) { if (!strncmp($space, '__', 2) || !is_array($_SESSION[$space])) { unset($spaces[$key]); } } } return new ArrayObject(array_merge($spaces, array_keys(parent::$_expiringData))); } /** * isWritable() - returns a boolean indicating if namespaces can write (use setters) * * @return bool */ public static function isWritable() { return parent::$_writable; } /** * isReadable() - returns a boolean indicating if namespaces can write (use setters) * * @return bool */ public static function isReadable() { return parent::$_readable; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -