* adjust for add tid for session.

This commit is contained in:
王怡栋
2021-08-30 10:56:52 +08:00
parent 1f068e6601
commit aa5a8bdd33
6 changed files with 147 additions and 8 deletions
+18 -3
View File
@@ -160,9 +160,12 @@ class baseHelper
public static function processOnlyBodyParam($link, $onlyBody = false)
{
global $config;
if(!$onlyBody and !self::inOnlyBodyMode()) return $link;
$onlybodyString = strpos($link, '?') === false ? "?onlybody=yes" : "&onlybody=yes";
return $link . $onlybodyString;
$sign = strpos($link, '?') === false ? "?" : "&";
$appendString = '';
if($onlyBody or self::inOnlyBodyMode()) $appendString = $sign . "onlybody=yes";
if(self::isWithTID()) $appendString .= empty($appendString) ? "{$sign}tid={$_GET['tid']}" : "&tid={$_GET['tid']}";
return $link . $appendString;
}
/**
@@ -177,6 +180,18 @@ class baseHelper
return (isset($_GET['onlybody']) and $_GET['onlybody'] == 'yes');
}
/**
* Is with tid.
*
* @static
* @access public
* @return bool
*/
public static function isWithTID()
{
return (!empty($config->tabSession) and isset($_GET['tid']));
}
/**
* 使用helper::import()来引入文件,不要直接使用include或者require.
* Using helper::import() to import a file, instead of include or require.
+122
View File
@@ -855,6 +855,16 @@ class baseRouter
{
if(defined('SESSION_STARTED')) return;
$ztSessionHandler = new ztSessionHandler(zget($_GET, 'tid', ''));
session_set_save_handler(
array($ztSessionHandler, "open"),
array($ztSessionHandler, "close"),
array($ztSessionHandler, "read"),
array($ztSessionHandler, "write"),
array($ztSessionHandler, "destroy"),
array($ztSessionHandler, "gc")
);
/* If request header has token, use it as session for authentication. */
if(isset($_SERVER['HTTP_TOKEN'])) session_id($_SERVER['HTTP_TOKEN']);
@@ -2616,3 +2626,115 @@ class super
if($this->scope == 'global') a($GLOBALS);
}
}
/**
* ZenTao session handler.
*
* @package framework
*/
class ztSessionHandler
{
public $sessSavePath;
public $tagID;
/**
* Construct.
*
* @param string $tagID
* @access public
* @return void
*/
public function __construct($tagID = '')
{
$this->tagID = $tagID;
}
/**
* Open
*
* @param string $savePath
* @param string $sessionName
* @access public
* @return bool
*/
public function open($savePath, $sessionName)
{
$this->sessSavePath = $savePath;
return true;
}
/**
* Close
*
* @access public
* @return bool
*/
public function close()
{
return true;
}
/**
* Read
*
* @param string $id
* @access public
* @return bool
*/
public function read($id)
{
$sessFile = "$this->sessSavePath/sess_$id" . $this->tagID;
if(!file_exists($sessFile))
{
($this->tagID and file_exists("$this->sessSavePath/sess_$id")) ? copy("$this->sessSavePath/sess_$id", $sessFile) : touch($sessFile);
}
return (string) file_get_contents($sessFile);
}
/**
* Write
*
* @param string $id
* @param string $sessData
* @access public
* @return bool
*/
public function write($id, $sessData)
{
$sessFile = "$this->sessSavePath/sess_$id" . $this->tagID;
if(file_put_contents($sessFile, $sessData)) return true;
return false;
}
/**
* Destroy
*
* @param string $id
* @access public
* @return bool
*/
public function destroy($id)
{
$sessFile = "$this->sessSavePath/sess_$id" . $this->tagID;
@unlink($sessFile);
touch($sessFile);
return true;
}
/**
* GC
*
* @param int $maxlifeTime
* @access public
* @return bool
*/
public function gc($maxlifeTime)
{
$time = time();
foreach(glob("$this->sessSavePath/sess_*") as $fileName)
{
if(filemtime($fileName) + $maxlifeTime < $time) @unlink($fileName);
}
return true;
}
}