I'm using a simple PHP-cache script which looks like that:
<?php
$url = $_SERVER["SCRIPT_NAME"];
$actual_link = $_SERVER['REQUEST_URI'];
$id = basename($actual_link);
$cachefile = 'cache/cached-items/cached-'.$id.'-content.html';
$cachetime = 2419200;
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
readfile($cachefile);
exit;
}
ob_start(); // Start the output buffer
// PHP Code which should get cached //
// Cache the contents to a cache file
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
// Another script which should't get cached
?>
If a cached file is available the script exits and stops to execute the rest of the php code which shouldn't get cached. Also it breaks the html of the entire page.
I'm searching a solution for the "exit" statement which doesn't stop the execution of following php code and doesn't break the html code of the entire page if a cached file is available.
CodePudding user response:
I don't know if I get you right, but I would do it this way:
<?php
$url = $_SERVER["SCRIPT_NAME"];
$actual_link = $_SERVER['REQUEST_URI'];
$id = basename($actual_link);
$cachefile = 'cache/cached-items/cached-'.$id.'-content.html';
$cachetime = 2419200;
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
readfile($cachefile);
}
else {
ob_start(); // Start the output buffer
// PHP Code which should get cached //
// Cache the contents to a cache file
$cached = fopen($cachefile, 'w');
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
}
// Another script which should't get cached
?>