如何在PHP中处理登录注销会话和gcache?
我使用gcache将整个网站页面缓存为HTML,然后我只读取它并向用户显示。看起来是这样的:如何在PHP中处理登录注销会话和gcache?,php,session,caching,Php,Session,Caching,我使用gcache将整个网站页面缓存为HTML,然后我只读取它并向用户显示。看起来是这样的: $enableCache = true; if ($enableCache) { include("gcache.php"); $cache = new gCache; $cache->folder = "temp/"; $id = join("", $_GET); $id = ereg_replace("[^A-Za-z0-9]", "", $id);
$enableCache = true;
if ($enableCache)
{
include("gcache.php");
$cache = new gCache;
$cache->folder = "temp/";
$id = join("", $_GET);
$id = ereg_replace("[^A-Za-z0-9]", "", $id);
$cache->contentId=$id;
$cache->timeout = 5;
/* its mean gCache cached the whole page */
/* so, gcache, decide if he must response the compressed or */
/* uncompressed cache*/
$cache->isPage = true;
if ($cache->Valid()) {
echo $cache->content;
die;
}
$cache->capture();
}
而且效果很好。现在我在站点的标题中添加了一个链接“login”。当person单击它时,它会将person重定向到他/她提供信用的子页面,并设置会话参数(在调用session_start之前):
此人将被重定向到mysite.com。但问题是他/她看到了从缓存中获取的站点。所以在右上角有一个“login”,但我设置了当设置了$\u SESSION['theUser']时,它应该显示它并提供一个注销链接。但它不会显示,因为它会返回给用户网站的未登录版本
如何解决这个问题?我认为,以这种方式使用缓存的原因是为了最小化服务器负载和响应时间。但这可以通过更简单的方式实现,不使用第三奇偶性插件/类/东西:
<?php
global $cachefile;
//encode cache file name with sha1
$cachefilename = sha1($_SERVER['REQUEST_URI']);
//remove trailing slash
$cachefile = rtrim(sys_get_temp_dir (),'\\/').DIRECTORY_SEPARATOR.$cachefilename.'.cache';
// How long to keep cache file?
$cachetime = 60*5; //5min
// Is cache file still fresh? If so, serve it.
if (file_exists($cachefile) && filesize($cachefile)>100 && time() - $cachetime < filemtime($cachefile) ) {
readfile($cachefile);
exit;
}
function cacheOutputtoFile() {
global $cachefile;
$webpage = ob_get_contents();
if ($fp = fopen($cachefile, 'w+'))
{
// Do an exclusive lock
if (flock($fp, LOCK_EX))
{
// Truncate file
ftruncate($fp, 0);
fwrite($fp, $webpage);
fclose($fp);
}
}
}
ob_start();
register_shutdown_function("cacheOutputtoFile");
<?php
global $cachefile;
//encode cache file name with sha1
$cachefilename = sha1($_SERVER['REQUEST_URI']);
//remove trailing slash
$cachefile = rtrim(sys_get_temp_dir (),'\\/').DIRECTORY_SEPARATOR.$cachefilename.'.cache';
// How long to keep cache file?
$cachetime = 60*5; //5min
// Is cache file still fresh? If so, serve it.
if (file_exists($cachefile) && filesize($cachefile)>100 && time() - $cachetime < filemtime($cachefile) ) {
readfile($cachefile);
exit;
}
function cacheOutputtoFile() {
global $cachefile;
$webpage = ob_get_contents();
if ($fp = fopen($cachefile, 'w+'))
{
// Do an exclusive lock
if (flock($fp, LOCK_EX))
{
// Truncate file
ftruncate($fp, 0);
fwrite($fp, $webpage);
fclose($fp);
}
}
}
ob_start();
register_shutdown_function("cacheOutputtoFile");
require_once("cacheable.php");