📄 session.php
字号:
<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @category Zend * @package Zend_Session * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @version $Id: Session.php 8064 2008-02-16 10:58:39Z thomas $ * @since Preview Release 0.2 *//** * @see Zend_Session_Abstract */require_once 'Zend/Session/Abstract.php';/** * @see Zend_Session_Namespace */require_once 'Zend/Session/Namespace.php';/** * @see Zend_Session_SaveHandler_Interface */require_once 'Zend/Session/SaveHandler/Interface.php';/** * Zend_Session * * @category Zend * @package Zend_Session * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */class Zend_Session extends Zend_Session_Abstract{ /** * Check whether or not the session was started * * @var bool */ private static $_sessionStarted = false; /** * Whether or not the session id has been regenerated this request. * * Id regeneration state * <0 - regenerate requested when session is started * 0 - do nothing * >0 - already called session_regenerate_id() * * @var int */ private static $_regenerateIdState = 0; /** * Private list of php's ini values for ext/session * null values will default to the php.ini value, otherwise * the value below will overwrite the default ini value, unless * the user has set an option explicity with setOptions() * * @var array */ private static $_defaultOptions = array( 'save_path' => null, 'name' => null, /* this should be set to a unique value for each application */ 'save_handler' => null, //'auto_start' => null, /* intentionally excluded (see manual) */ 'gc_probability' => null, 'gc_divisor' => null, 'gc_maxlifetime' => null, 'serialize_handler' => null, 'cookie_lifetime' => null, 'cookie_path' => null, 'cookie_domain' => null, 'cookie_secure' => null, 'use_cookies' => null, 'use_only_cookies' => 'on', 'referer_check' => null, 'entropy_file' => null, 'entropy_length' => null, 'cache_limiter' => null, 'cache_expire' => null, 'use_trans_sid' => null, 'bug_compat_42' => null, 'bug_compat_warn' => null, 'hash_function' => null, 'hash_bits_per_character' => null ); /** * List of options pertaining to Zend_Session that can be set by developers * using Zend_Session::setOptions(). This list intentionally duplicates * the individual declaration of static "class" variables by the same names. * * @var array */ private static $_localOptions = array( 'strict' => '_strict', 'remember_me_seconds' => '_rememberMeSeconds' ); /** * Whether or not write close has been performed. * * @var bool */ private static $_writeClosed = false; /** * Whether or not session id cookie has been deleted * * @var bool */ private static $_sessionCookieDeleted = false; /** * Whether or not session has been destroyed via session_destroy() * * @var bool */ private static $_destroyed = false; /** * Whether or not session must be initiated before usage * * @var bool */ private static $_strict = false; /** * Default number of seconds the session will be remembered for when asked to be remembered * * @var int */ private static $_rememberMeSeconds = 1209600; // 2 weeks /** * Whether the default options listed in Zend_Session::$_localOptions have been set * * @var bool */ private static $_defaultOptionsSet = false; /** * A reference to the set session save handler * * @var Zend_Session_SaveHandler_Interface */ private static $_saveHandler = null; /** * Constructor overriding - make sure that a developer cannot instantiate */ private function __construct() { } /** * setOptions - set both the class specified * * @param array $userOptions - pass-by-keyword style array of <option name, option value> pairs * @throws Zend_Session_Exception * @return void */ public static function setOptions(array $userOptions = array()) { // set default options on first run only (before applying user settings) if (!self::$_defaultOptionsSet) { foreach (self::$_defaultOptions as $defaultOptionName => $defaultOptionValue) { if (isset(self::$_defaultOptions[$defaultOptionName])) { ini_set("session.$defaultOptionName", $defaultOptionValue); } } self::$_defaultOptionsSet = true; } // set the options the user has requested to set foreach ($userOptions as $userOptionName => $userOptionValue) { $userOptionName = strtolower($userOptionName); // set the ini based values if (array_key_exists($userOptionName, self::$_defaultOptions)) { ini_set("session.$userOptionName", $userOptionValue); } elseif (isset(self::$_localOptions[$userOptionName])) { self::${self::$_localOptions[$userOptionName]} = $userOptionValue; } else { /** @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception("Unknown option: $userOptionName = $userOptionValue"); } } } /** * setSaveHandler() - Session Save Handler assignment * * @param Zend_Session_SaveHandler_Interface $interface * @return void */ public static function setSaveHandler(Zend_Session_SaveHandler_Interface $saveHandler) { session_set_save_handler( array(&$saveHandler, 'open'), array(&$saveHandler, 'close'), array(&$saveHandler, 'read'), array(&$saveHandler, 'write'), array(&$saveHandler, 'destroy'), array(&$saveHandler, 'gc') ); self::$_saveHandler = $saveHandler; } /** * getSaveHandler() - Get the session Save Handler * * @return Zend_Session_SaveHandler_Interface */ public static function getSaveHandler() { return self::$_saveHandler; } /** * regenerateId() - Regenerate the session id. Best practice is to call this after * session is started. If called prior to session starting, session id will be regenerated * at start time. * * @throws Zend_Session_Exception * @return void */ public static function regenerateId() { 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 (self::$_sessionStarted && self::$_regenerateIdState <= 0) { session_regenerate_id(true); self::$_regenerateIdState = 1; } else { /** * @todo If we can detect that this requester had no session previously, * then why regenerate the id before the session has started? * Feedback wanted for: // if (isset($_COOKIE[session_name()]) || (!use only cookies && isset($_REQUEST[session_name()]))) { self::$_regenerateIdState = 1; } else { self::$_regenerateIdState = -1; } //*/ self::$_regenerateIdState = -1; } } /** * rememberMe() - Write a persistent cookie that expires after a number of seconds in the future. If no number of * seconds is specified, then this defaults to self::$_rememberMeSeconds. Due to clock errors on end users' systems, * large values are recommended to avoid undesirable expiration of session cookies. * * @param $seconds integer - OPTIONAL specifies TTL for cookie in seconds from present time * @return void */ public static function rememberMe($seconds = null) { $seconds = (int) $seconds; $seconds = ($seconds > 0) ? $seconds : self::$_rememberMeSeconds; self::rememberUntil($seconds); } /** * forgetMe() - Write a volatile session cookie, removing any persistent cookie that may have existed. The session * would end upon, for example, termination of a web browser program. * * @return void */ public static function forgetMe() { self::rememberUntil(0); } /** * rememberUntil() - This method does the work of changing the state of the session cookie and making * sure that it gets resent to the browser via regenerateId() * * @param int $seconds * @return void */ public static function rememberUntil($seconds = 0) { $cookieParams = session_get_cookie_params(); session_set_cookie_params( $seconds, $cookieParams['path'], $cookieParams['domain'], $cookieParams['secure'] ); // normally "rememberMe()" represents a security context change, so should use new session id self::regenerateId(); } /** * sessionExists() - whether or not a session exists for the current request * * @return bool */ public static function sessionExists() { if (ini_get('session.use_cookies') == '1' && isset($_COOKIE[session_name()])) { return true; } elseif (!empty($_REQUEST[session_name()])) { return true; } return false; } /** * start() - Start the session. * * @param bool|array $options OPTIONAL Either user supplied options, or flag indicating if start initiated automatically * @throws Zend_Session_Exception * @return void */ public static function start($options = false) { if (self::$_sessionStarted && self::$_destroyed) { require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception('The session was explicitly destroyed during this request, attempting to re-start is not allowed.'); } if (self::$_sessionStarted) { return; // already started } // make sure our default options (at the least) have been set if (!self::$_defaultOptionsSet) { self::setOptions(is_array($options) ? $options : array()); } // In strict mode, do not allow auto-starting Zend_Session, such as via "new Zend_Session_Namespace()" if (self::$_strict && $options === true) { /** @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception('You must explicitly start the session with Zend_Session::start() when session options are set to strict.'); } if (headers_sent($filename, $linenum)) { /** @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php'; throw new Zend_Session_Exception("Session must be started before any output has been sent to the browser;" . " output started in {$filename}/{$linenum}"); } // See http://www.php.net/manual/en/ref.session.php for explanation if (defined('SID')) { /** @see Zend_Session_Exception */ require_once 'Zend/Session/Exception.php';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -