📄 session.php
字号:
* @access public * @param string $name Name of variable * @param string $namespace Namespace to use, default to 'default' * @return mixed $value the value from session or NULL if not set */ function clear( $name, $namespace = 'default' ) { $namespace = '__'.$namespace; //add prefix to namespace to avoid collisions if( $this->_state !== 'active' ) { // @TODO :: generated error here return null; } $value = null; if( isset( $_SESSION[$namespace][$name] ) ) { $value = $_SESSION[$namespace][$name]; unset( $_SESSION[$namespace][$name] ); } return $value; } /** * Start a session * * Creates a session (or resumes the current one based on the state of the session) * * @access private * @return boolean $result true on success */ function _start() { // start session if not startet if( $this->_state == 'restart' ) { session_id( $this->_createId() ); } session_cache_limiter('none'); session_start(); // Send modified header for IE 6.0 Security Policy header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"'); return true; } /** * Frees all session variables and destroys all data registered to a session * * This method resets the $_SESSION variable and destroys all of the data associated * with the current session in its storage (file or DB). It forces new session to be * started after this method is called. It does not unset the session cookie. * * @static * @access public * @return void * @see session_unset() * @see session_destroy() */ function destroy() { // session was already destroyed if( $this->_state === 'destroyed' ) { return true; } // In order to kill the session altogether, like to log the user out, the session id // must also be unset. If a cookie is used to propagate the session id (default behavior), // then the session cookie must be deleted. if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time()-42000, '/'); } session_unset(); session_destroy(); $this->_state = 'destroyed'; return true; } /** * restart an expired or locked session * * @access public * @return boolean $result true on success * @see destroy */ function restart() { $this->destroy(); if( $this->_state !== 'destroyed' ) { // @TODO :: generated error here return false; } // Re-register the session handler after a session has been destroyed, to avoid PHP bug $this->_store->register(); $this->_state = 'restart'; //regenerate session id $id = $this->_createId( strlen( $this->getId() ) ); session_id($id); $this->_start(); $this->_state = 'active'; $this->_validate(); $this->_setCounter(); return true; } /** * Create a new session and copy variables from the old one * * @abstract * @access public * @return boolean $result true on success */ function fork() { if( $this->_state !== 'active' ) { // @TODO :: generated error here return false; } // save values $values = $_SESSION; // keep session config $trans = ini_get( 'session.use_trans_sid' ); if( $trans ) { ini_set( 'session.use_trans_sid', 0 ); } $cookie = session_get_cookie_params(); // create new session id $id = $this->_createId( strlen( $this->getId() ) ); // kill session session_destroy(); // re-register the session store after a session has been destroyed, to avoid PHP bug $this->_store->register(); // restore config ini_set( 'session.use_trans_sid', $trans ); session_set_cookie_params( $cookie['lifetime'], $cookie['path'], $cookie['domain'], $cookie['secure'] ); // restart session with new id session_id( $id ); session_start(); return true; } /** * Writes session data and ends session * * Session data is usually stored after your script terminated without the need * to call JSession::close(),but as session data is locked to prevent concurrent * writes only one script may operate on a session at any time. When using * framesets together with sessions you will experience the frames loading one * by one due to this locking. You can reduce the time needed to load all the * frames by ending the session as soon as all changes to session variables are * done. * * @access public * @see session_write_close() */ function close() { session_write_close(); } /** * Create a session id * * @static * @access private * @return string Session ID */ function _createId( ) { $id = 0; while (strlen($id) < 32) { $id .= mt_rand(0, mt_getrandmax()); } $id = md5( uniqid($id, true)); return $id; } /** * Set session cookie parameters * * @access private */ function _setCookieParams() { $cookie = session_get_cookie_params(); if($this->_force_ssl) { $cookie['secure'] = true; } session_set_cookie_params( $cookie['lifetime'], $cookie['path'], $cookie['domain'], $cookie['secure'] ); } /** * Create a token-string * * @access protected * @param int $length lenght of string * @return string $id generated token */ function _createToken( $length = 32 ) { static $chars = '0123456789abcdef'; $max = strlen( $chars ) - 1; $token = ''; $name = session_name(); for( $i = 0; $i < $length; ++$i ) { $token .= $chars[ (rand( 0, $max )) ]; } return md5($token.$name); } /** * Set counter of session usage * * @access protected * @return boolean $result true on success */ function _setCounter() { $counter = $this->get( 'session.counter', 0 ); ++$counter; $this->set( 'session.counter', $counter ); return true; } /** * Set the session timers * * @access protected * @return boolean $result true on success */ function _setTimers() { if( !$this->has( 'session.timer.start' ) ) { $start = time(); $this->set( 'session.timer.start' , $start ); $this->set( 'session.timer.last' , $start ); $this->set( 'session.timer.now' , $start ); } $this->set( 'session.timer.last', $this->get( 'session.timer.now' ) ); $this->set( 'session.timer.now', time() ); return true; } /** * set additional session options * * @access protected * @param array $options list of parameter * @return boolean $result true on success */ function _setOptions( &$options ) { // set name if( isset( $options['name'] ) ) { session_name( md5($options['name']) ); } // set id if( isset( $options['id'] ) ) { session_id( $options['id'] ); } // set expire time if( isset( $options['expire'] ) ) { $this->_expire = $options['expire']; } // get security options if( isset( $options['security'] ) ) { $this->_security = explode( ',', $options['security'] ); } if( isset( $options['force_ssl'] ) ) { $this->_force_ssl = (bool) $options['force_ssl']; } //sync the session maxlifetime ini_set('session.gc_maxlifetime', $this->_expire); return true; } /** * Do some checks for security reason * * - timeout check (expire) * - ip-fixiation * - browser-fixiation * * If one check failed, session data has to be cleaned. * * @access protected * @param boolean $restart reactivate session * @return boolean $result true on success * @see http://shiflett.org/articles/the-truth-about-sessions */ function _validate( $restart = false ) { // allow to restart a session if( $restart ) { $this->_state = 'active'; $this->set( 'session.client.address' , null ); $this->set( 'session.client.forwarded' , null ); $this->set( 'session.client.browser' , null ); $this->set( 'session.token' , null ); } // check if session has expired if( $this->_expire ) { $curTime = $this->get( 'session.timer.now' , 0 ); $maxTime = $this->get( 'session.timer.last', 0 ) + $this->_expire; // empty session variables if( $maxTime < $curTime ) { $this->_state = 'expired'; return false; } } // record proxy forwarded for in the session in case we need it later if( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { $this->set( 'session.client.forwarded', $_SERVER['HTTP_X_FORWARDED_FOR']); } // check for client adress if( in_array( 'fix_adress', $this->_security ) && isset( $_SERVER['REMOTE_ADDR'] ) ) { $ip = $this->get( 'session.client.address' ); if( $ip === null ) { $this->set( 'session.client.address', $_SERVER['REMOTE_ADDR'] ); } else if( $_SERVER['REMOTE_ADDR'] !== $ip ) { $this->_state = 'error'; return false; } } // check for clients browser if( in_array( 'fix_browser', $this->_security ) && isset( $_SERVER['HTTP_USER_AGENT'] ) ) { $browser = $this->get( 'session.client.browser' ); if( $browser === null ) { $this->set( 'session.client.browser', $_SERVER['HTTP_USER_AGENT']); } else if( $_SERVER['HTTP_USER_AGENT'] !== $browser ) {// $this->_state = 'error';// return false; } } return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -