chaching in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sathish.hc
Forum Newbie
Posts: 15
Joined: Tue May 23, 2006 2:55 am

chaching in PHP

Post by sathish.hc »

Hi all,

Can any one help how to chache a web page for 24 hours in PHP
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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");
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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);

?>
sathish.hc
Forum Newbie
Posts: 15
Joined: Tue May 23, 2006 2:55 am

Post by sathish.hc »

Thank you very much both of you I need both
Post Reply