Home >Backend Development >PHP Tutorial >Completely solve the problem of PHP Session not expiring and SessionId remaining unchanged_PHP Tutorial

Completely solve the problem of PHP Session not expiring and SessionId remaining unchanged_PHP Tutorial

WBOY
WBOYOriginal
2016-07-14 10:10:381272browse

After using the session in asp.net and then using the session in php, you will feel that the session in php is so uncomfortable compared to the session in asp.net. When using PHP sessions, you may encounter problems such as the session not invalidating, closing the browser and the session still exists, reopening the browser and the sessionid remains the same as before. . .


Let’s take a look at the session mechanism of PHP:

session recycling mechanism:

PHP uses the Garbage Collection process to recycle expired sessions. However, not every time a session is established, the 'garbage collection' process can be invoked. GC is started according to a certain probability. This is mainly due to server performance considerations. Each session triggers gc. If the number of views is large, the server cannot bear it. However, gc is enabled according to a certain probability. When the number of views is large, the session expiration mechanism can operate normally, and Server efficiency is saved. The details should all come from years of experience.

Three parameters related to PHP session expiration (in php.ini):

session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440

GC startup probability = gc_probability / gc_divisor = 0.1%

session expiration time gc_maxlifetime unit: seconds


When the web service is officially provided, the session expiration probability needs to be comprehensively considered based on the number of views of the web service and the performance of the server. It is obviously unwise to enable gc for each session. It feels a bit "lucky". If the number of visits is small, the chance of hitting is small. During my testing on this machine, I almost never got hit, and the sessionid remained unchanged for several days, even if the machine was restarted. During the test, the expiration probability value should be set larger to increase the hit probability.


By modifying the expiration probability value of the PHP configuration file, you can set the session expiration in a "lucky" manner. Is there a better way?

The session class written below can completely solve the problem of session not expiring and sessionid unchanged.

[php]
 
 
/**
* Extended Session class (simple encapsulation)
*
* @author slimboy
*
​*/ 
class Session { 
 
    /**
* Initialization
​​*/ 
    static function _init(){ 
        ini_set('session.auto_start', 0); 
        //Session::start();  
    } 
     
    /**
* Start Session
​​*/ 
    static function start() { 
        session_start(); 
    } 
 
    /**
* Set Session
* *
* @param $name Session name
* @param $value value
* @param $time timeout (seconds)
​​*/ 
    public static function set($name,$value,$time){ 
        if(empty($time)){ 
            $time = 1800; //默认值  
        } 
        $_SESSION[$name] = $value; 
        $_SESSION[$name.'_Expires'] = time() + $time; 
    } 
     
    /**
* Get Session value
* *
* @param $name Session name
​​*/ 
    public static function get($name){ 
        //检查Session是否已过期  
        if(isset($_SESSION[$name.'_Expires']) && $_SESSION[$name.'_Expires']>time()){ 
            return $_SESSION[$name]; 
        }else{ 
            Session::clear($name); 
            return null; 
        } 
    } 
     
     
    /**
* Set Session Domain
* *
* @param $sessionDomain domain
* @return string
​​*/ 
    static function setDomain($sessionDomain = null) { 
        $return = ini_get('session.cookie_domain'); 
        if(!empty($sessionDomain)) { 
            ini_set('session.cookie_domain', $sessionDomain);//跨域访问Session  
        } 
        return $return; 
    } 
     
     
    /**
* Clear a certain Session value
* *
* @param $name Session name
​​*/ 
    static function clear($name){ 
        unset($_SESSION[$name]); 
        unset($_SESSION[$name.'_Expires']); 
    } 
     
     
    /**
* Reset and destroy Session
​​*/ 
    static function destroy(){ 
        unset($_SESSION); 
        session_destroy(); 
    } 
     
     
    /**
     * 获取或设置Session id
     */ 
    static function sessionid($id=null){ 
        return session_id($id); 
    } 
 

 
Session::_init(); 


/**
* Extended Session class (simple encapsulation)
*
* @author slimboy
*
​*/
class Session {

 /**
* Initialization
​*/
 static function _init(){
  ini_set('session.auto_start', 0);
  //Session::start();
 }
 
    /**
* Start Session
​​*/
    static function start() {
        session_start();
    }

    /**
* Set Session
*
* @param $name Session name
* @param $value value
* @param $time timeout (seconds)
​​*/
    public static function set($name,$value,$time){
     if(empty($time)){
      $time = 1800; //默认值
     }
     $_SESSION[$name] = $value;
     $_SESSION[$name.'_Expires'] = time() + $time;
    }
   
    /**
* Get Session value
*
* @param $name Session name
​​*/
    public static function get($name){
     //检查Session是否已过期
     if(isset($_SESSION[$name.'_Expires']) && $_SESSION[$name.'_Expires']>time()){
      return $_SESSION[$name];
     }else{
      Session::clear($name);
      return null;
     }
    }
   
   
    /**
* * Set Session Domain
*
* @param $sessionDomain domain
* @return string
​​*/
    static function setDomain($sessionDomain = null) {
     $return = ini_get('session.cookie_domain');
     if(!empty($sessionDomain)) {
      ini_set('session.cookie_domain', $sessionDomain);//跨域访问Session
     }
     return $return;
    }
   
   
    /**
* Clear a certain Session value
*
* @param $name Session name
​​*/
    static function clear($name){
     unset($_SESSION[$name]);
     unset($_SESSION[$name.'_Expires']);
    }
   
   
    /**
* Reset and destroy Session
​​*/
    static function destroy(){
     unset($_SESSION);
     session_destroy();
    }
   
   
    /**
* Get or set Session id
​​*/
    static function sessionid($id=null){
     return session_id($id);
    }

}

Session::_init();

 

 

调用示例:


[php]
//设置session  
Session::set('UserId', $userid, 3600); 

//设置session
Session::set('UserId', $userid, 3600);
[php]
//读取session  
$userId = Session::get('UserId'); 

//读取session
$userId = Session::get('UserId');

 

 

当然,应该还有其它办法,欢迎童鞋提成!

 


 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477484.htmlTechArticleAfter using the session in asp.net and then using the session in php, you will feel that compared with the session in php The session in asp.net is so uncomfortable. When using PHP session, you may encounter sess...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn