session_set_save_handler session存入redis\数据库

redis存储session

redis = new Redis();
    $this->redis->connect('127.0.0.1',6379);  //连接redis
    ini_set('session.save_handler','user');
    $retval = session_set_save_handler(
      array($this,"open"),
      array($this,"close"),
      array($this,"read"),
      array($this,"write"),
      array($this,"destory"),
      array($this,"gc")
    );
      session_start();
  }
    
    public function open($path,$name){
      return true;
    }
    public function close(){
      return true;
    }
    public function read($id){
      $value = $this->redis->get($id);
      if($value){
        return $value;
      }else{
        return "";
      }
    }
    public function write($id,$data){
      if($this->redis->set($id,$data)){
        $this->redis->expire($id,$this->sessionExpireTime); 
         //设置过期时间
        return true;
      }
      return false;
    }
    public function destory($id){
      if($this->redis->delete($id)){
        return true;
      }
      return false;
    }
    public function gc($maxlifetime){
      return true;
    }
    //析构函数
    public function __destruct(){
      session_write_close();
    }
      
}
  
  
$re = new SessionManager();
$_SESSION['name'] = "qq";
echo $_SESSION['name'];

mysql存储session

DB_NAME = &$sessionDB;
  $this->SESS_LIFE = get_cfg_var("session.gc_maxlifetime");
  ini_set('session.save_handler','user');
  session_module_name('user');
  session_set_save_handler( 
  array(&$this, 'sess_open'), 
  array(&$this, 'sess_close'), 
  array(&$this, 'sess_read'), 
  array(&$this, 'sess_write'), 
  array(&$this, 'sess_destroy'), 
  array(&$this, 'sess_gc') 
  );
  session_start();
}
function sess_open($save_path, $session_name){  // 打开数据库连接
  if (! $this->DB_SELECT_DB = mysql_pconnect($this->DB_SERVER, $this->DB_USER, $this->DB_PASS)) { 
  echo "SORRY! MYSQL ERROR : Can't connect to $this->DB_SERVER as $DB_USER"; 
  echo "MySQL Error: ", mysql_error(); 
  die; 
  } 
  if (! mysql_select_db($this->DB_NAME, $this->DB_SELECT_DB)) { 
  echo "SORRY! MYSQL ERROR : Unable to select database $this->DB_NAME"; 
  die; 
  } 
  return true; 
} 
function sess_close() { 
    return true; 
} 
function sess_read($SessionKey){  
    $Query = "SELECT SessionArray FROM sessions WHERE SessionKey = '".$SessionKey."' AND SessionExpTime > " . time();
    // 过期不读取。
    $Result = mysql_query($Query, $this->DB_SELECT_DB); 
  if (list($SessionArray) = mysql_fetch_row($Result)) { 
     return $SessionArray; 
  } 
  return false; 
} 
function sess_write($SessionKey, $VArray) { 
  $SessionExpTime = time() + $this->SESS_LIFE;
  // 更新Session过期时间,Session过期时间 = 最后一次更新时间 + Session的最大使用时长
  $SessionArray = addslashes($VArray); 
  $Query = "INSERT INTO sessions (SessionKey,SessionExpTime,SessionArray) VALUES ('".$SessionKey."','".$SessionExpTime."','".$SessionArray."')"; 
  $Result = mysql_query($Query, $this->DB_SELECT_DB); 
  if (!$Result){ 
      $Query = "UPDATE sessions SET SessionExpTime = '".$SessionExpTime."', SessionArray = '".$SessionArray."' WHERE SessionKey = '".$SessionKey."' AND  SessionExpTime > " . time(); 
      $Result = mysql_query($Query, $this->DB_SELECT_DB); 
  }  
  return $Result; 
} 
function sess_destroy($SessionKey) { 
  $Query = "DELETE FROM sessions WHERE SessionKey = '".$SessionKey."'"; 
  $Result = mysql_query($Query, $this->DB_SELECT_DB); 
  return $Result; 
} 
function sess_gc($maxlifetime) {
// 这个垃圾清除器系统调用。默认是1440秒清除一次。
//参数可以在PHP.ini里面设置。
  $Query = "DELETE FROM sessions WHERE SessionExpTime < " . time(); 
  $Result = mysql_query($Query, $this->DB_SELECT_DB); 
  return mysql_affected_rows($this->DB_SELECT_DB); 
} 
}
?>

你可能感兴趣的:(php)