EasyPHPScripts.com      Free Open Source PHP Scripts and Code Snippet Library

 +Menu
+   Site News
+   Main Pages
+   Other Downloads
 +PHP
+ Arrays (9)
+ Directory And Files (7)
+ Image (3)
+ LDAP (3)
+ MySQL (11)
+ Other (11)
+ Regular Expressions (3)
+ String Manipulation (13)
+ Time and Date (6)
 +Library Options
+   View Other Library
 +General Options
+   Log in

Main Pages

Downlaods -|- Contact
Home
EasyPHPNews
EasyPHPTabs
EasyPHPTemplate
EasyPHPEdit
Flat file CMS
EasyPHPCashe
EasyPHPNews Discussions
Fix Vista PHP CLI Errors
Contact

A simple php cashe using a php buffer. Just some basics to get the ball rolling.
i made a class from this, Download EasyPHPCashe

First thing we need to know is the page name to be cashed. and avoid page creation errors by cleaning the name. use of $_SERVER['REQUEST_URI'] enables dynamic pages.

this code would be in the top or header page.

//fix for IIS
if(!isset($_SERVER['REQUEST_URI'])) {
if(isset($_SERVER['SCRIPT_NAME']))
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
else
$_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'];
if($_SERVER['QUERY_STRING']){
$_SERVER['REQUEST_URI'] .= '?'.$_SERVER['QUERY_STRING'];
}
}
//end IIS fix

//pull everything out that is not aplha,numeric. and add the storage dir.
Also we dont want to cashe post pages, you may have to add some codeing depending to stop the cashe on certian pages. or dont add to those pages.
like if($_POST) $cashe off, dont want to cashe posted stuff...nor display pages before the post.

$cashe_page = 'cashe/'.eregi_replace("[^A-Z,0-9]", '', $_SERVER['REQUEST_URI']).'.txt';
//seconds
$cashe_lifetime = 3600;


if(is_file($cashe_page)){
//last time the file was modified minus time now plus cashe life time.
$cahse_filetime = filemtime($cashe_page) - time() + $cashe_lifetime;
if($cahse_filetime > 0){
if(@include $cashe_page)
exit;
} else $cashe_it = 1;
} else $cashe_it = 1;


//at top the page just before where content starts
if($_POST) $cashe_it = 0;
if($cashe_it){
ob_start();
}


//content

some content


//footer or the end of the page. write buffer to file.
//file_put_contents is php ver 5+
if($cashe_it){
//get buffer data
$ob_end = ob_get_contents();
file_put_contents($cashe_page,$ob_end);
}

Downlaods
Powered By PHP
 
Powered by: PHP & centOS