<?php
$url = 'http://www.site.com/file.php';
$dest_file = 'footercache.txt'; //be sure it is chmod-ed to 0666 !!
//Note: for requests by keyword use a cache folder whose rights are 0777 and define the destination file as
//$dest_file = CACHE_FOLDER . '/cache-' . preg_replace('/[^a-z0-9]+/','_', $keyword) . '.txt';
//this may need changes on the line checking the length of the dest_file
$pagesource = request_cache($url, $dest_file, 3600*24*7);
echo $pagesource;
function request_cache($url, $dest_file, $timeout=7200) {
if(strlen($dest_file) > 100) $dest_file = substr($dest_file, 20, 60) . substr($dest_file, strlen($dest_file) - 4);//keep some chars and the probable extension
if(!file_exists($dest_file) || filemtime($dest_file) < (time()-$timeout)) {
$data = @file_get_contents($url);
if ($data !== false) {
$tmpf = tempnam('/tmp','YWS');
$fp = @fopen($tmpf,"w");
@fwrite($fp, $data);
@fclose($fp);
if(file_exists($dest_file)) @unlink($dest_file);
rename($tmpf, $dest_file);
}else{
touch($dest_file);//update its date only
}
} else {
$data = file_get_contents($dest_file);
}
return($data);
}
?>
Snippet codes and other stuff good to remember. Various source codes, tips and other things I encountered while working
Monday, March 12, 2007
php cache
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment