Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP页面的简单缓存代码失败-提交表单时如何使缓存无效_Php_Caching - Fatal编程技术网

PHP页面的简单缓存代码失败-提交表单时如何使缓存无效

PHP页面的简单缓存代码失败-提交表单时如何使缓存无效,php,caching,Php,Caching,这是我在动态网站db文件中使用的缓存代码。每一页的顶部都包含这段代码 <?php // cache - will work online - not locally // location and prefix for cache files define('CACHE_PATH', "siteCache/"); // how long to keep the cache files (hours) define('CACHE_TIME', 12); // return locati

这是我在动态网站db文件中使用的缓存代码。每一页的顶部都包含这段代码

<?php
// cache - will work online - not locally
// location and prefix for cache files
define('CACHE_PATH', "siteCache/"); 

// how long to keep the cache files (hours)
define('CACHE_TIME', 12); 
// return location and name for cache file 
function cache_file() 
{ 
    return CACHE_PATH . md5($_SERVER['REQUEST_URI']); 
}

// display cached file if present and not expired 
function cache_display() 
{ 
    $file = cache_file(); 
    // check that cache file exists and is not too old 
    if(!file_exists($file)) return; 
    if(filemtime($file) < time() - CACHE_TIME * 3600) return; 
    // if so, display cache file and stop processing
    readfile($file);
    exit; 
} 

// write to cache file 
function cache_page($content) 
{ 
    if(false !== ($f = @fopen(cache_file(), 'w'))) { 
        fwrite($f, $content);
        fclose($f); 
    } 
    return $content; 
} 

// execution stops here if valid cache file found
cache_display(); 

// enable output buffering and create cache file 
ob_start('cache_page');
?>

页面正在被缓存,它正在工作,但在表单提交、用户登录、通过页面的变量上,什么都没有发生。正在显示旧页面。我如何使用这个缓存代码,使它可以工作,但网站仍然保持功能

我想知道wordpress插件是如何做到这一点的。Wp超级缓存和W3T缓存缓存所有内容,但blog仍然可以正常工作。我是否应该在网站的某些部分选择性地使用它

像这样:

<?php session_start();
include("db.php"); ?>

但它也不起作用,因为它是关于pageURL(整页缓存),而不是来自页面的选择性内容

请告知。有什么简单的脚本可以做到这一点。Pear::Cache_Lite看起来不错,但它看起来很难实现


更新:我使用了Cache_-Lite。都一样。缓存所有内容或包含的php文件。可使用的配置选项很少。但如果作为一个整体使用,它还将忽略get、post、会话数据更新……并将显示以前的缓存页面,除非它们被删除。

我认为可以将显示与逻辑分开

我的意思是,更改表单的action属性,并将其指向一个没有缓存逻辑的php(并且必须使用令牌或会话等检查referer或其他参数,以避免CSRF之类的安全问题)

我想指出的另一件事是,你应该只缓存访问量最大的页面(即主页),通常你没有“一刀切”的缓存,最好不要担心没有速度/负载问题的页面。或者,如果您的速度问题来自数据库查询,则最好缓存数据(您应该在实现缓存之前评测应用程序)


migth工作的另一种方法是检查请求方法并禁用缓存,如果它是post(假设您的所有表单都使用post方法),请使用
$\u服务器['request\u method']='post'

每个页面都有位于顶部的会话。主页已获得登录表单。现在主页被缓存了。我登录,返回主页,因为主页已经缓存了旧的空登录表单部分,它显示空登录部分-否则,如果登录,它应该有登录用户的问候消息。我唯一想到的是,在提交任何表单后,总是立即清空缓存文件夹,然后重定向回。在这种情况下,您必须在成功登录后使缓存文件无效(即:删除)。问题是,您的用户将看到其他用户的主页。您可以为每个用户提供一个主页缓存文件,但我认为它脏而且有些无用。我仍然认为,您将从数据库缓存而不是html缓存中受益更多,而且实现起来更容易。如果你有很多相对静态的页面,或者如果你有一些消耗资源的重模板系统,Html缓存是很好的。同样,我建议你分析一下应用程序,看看什么是需要时间加载的。没什么特别的,只是使用microtime()来测量数据库查询、php执行等的时间。在开始html缓存之前,您甚至可以找到更好的方法来处理速度/负载问题(可能是重写查询,或者向表中添加索引)。
<?php
// TOP of your script
$cachefile = 'cache/'.basename($_SERVER['SCRIPT_URI']);
$cachetime = 120 * 60; // 2 hours

// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
    include($cachefile);
    echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";
    exit;
}

ob_start(); // start the output buffer
// Your normal PHP script and HTML content here
// BOTTOM of your script
$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser
?>