答案 0 :(得分:3)
答案 1 :(得分:0)
function resetCache(){
restoreCacheSession();
session_unset();
restoreTrueSession();
}
function restoreCacheSession(){
$sessionId = session_id();
if(strlen($sessionId)) {
$origSetting = ini_get('session.use_cookies');
session_write_close();
}
session_id('cache');
ini_set('session.use_cookies', false);
session_start();
if($sessionId)
{
$_SESSION["_trueSessionId"] = $sessionId;
$_SESSION["_trueSessionSettings"] = $origSetting;
}
}
function restoreTrueSession(){
if(isset($_SESSION["_trueSessionId"])){
$sessionId = $_SESSION["_trueSessionId"];
$origSetting = $_SESSION["_trueSessionId"];
}
session_write_close();
if(isset($sessionId)) {
ini_set('session.use_cookies', $origSetting);
session_id($sessionId);
session_start();
}
elseif(isset($_COOKIE['phpSESSID'])){
session_id($_COOKIE['phpSESSID']);
session_start();
}
else {
session_start();
session_unset();
session_regenerate_id();
}
}
function cache($var, $value = null){
restoreCacheSession();
if(!isset($value)){
if(isset($_SESSION[$var])){
$result = $_SESSION[$var];
}
restoreTrueSession();
return isset($result)?$result:null;
}
$_SESSION[$var] = $value;
restoreTrueSession();
}
要在缓存中设置变量,您只需<?php cache("yourvarname",yourvarvalue) ?>
要在缓存中获取变量的值:<?php cache("yourvarname") ?>
要重置缓存<?php resetCache("yourvarname") ?>