Page 1 of 1

chaching in PHP

Posted: Mon Jul 31, 2006 4:44 am
by sathish.hc
Hi all,

Can any one help how to chache a web page for 24 hours in PHP

Posted: Mon Jul 31, 2006 5:52 am
by anjanesh

Code: Select all

header("Expires: " . date("D, j M Y H:i:s", time() + 86400) . " UTC");
header("Cache-Control: Public");
header("Pragma: Public");

Posted: Mon Jul 31, 2006 5:59 am
by bokehman
The above is client cache... I'm sure that's not what you meant but maybe it is. My guess is you are talking serverside caching of dynamic pages.

Code: Select all

<?php

ob_start();

$cache_dir = $_SERVER['DOCUMENT_ROOT'].'/cache/';
$self_cached = $cache_dir.preg_replace('/[^0-9a-zA-Z-_]/', '', $_SERVER['REQUEST_URI']);
if(is_file($self_cached) and (filemtime($self_cached) > strtotime(date('Y-m-d'))))
{
	readfile($self_cached);
	die;
}

/*

Page content here

*/

$buffer = ob_get_clean();
echo $buffer;
$fp = fopen($self_cached, 'w');
fwrite($fp, $buffer, strlen($buffer));
fclose($fp);

?>

Posted: Mon Jul 31, 2006 6:15 am
by sathish.hc
Thank you very much both of you I need both