Click here to Skip to main content
11,805,035 members (63,325 online)
Click here to Skip to main content

Tagged as

Caching Output in PHP

, 23 Dec 2009 CPOL 22.1K 8
Rate this:
Please Sign up or sign in to vote.


High-traffic sites can often benefit from caching of pages, to save processing of the same data over and over again.

Using the Code

Put the first function cache_start in the beginning of php script and the second cache_end in the end of script.

$_time : cache time
$dir : directory to cache files
function cache_start($_time, $dir)
  $cachefile = $dir.'/'.sha1($_SERVER['REQUEST_URI']).'.html';
  $cachetime = $_time;
  if(file_exists($cachefile) && (time( )-$cachetime < filemtime($cachefile)))
function cache_end($dir)
  $cachefile = $dir.'/'.sha1($_SERVER['REQUEST_URI']).'.html';
  $fp = fopen($cachefile, 'w');
  fwrite($fp, ob_get_contents());


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Greece Greece
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralUm Pin
Rockyonfire16-Aug-10 11:24
memberRockyonfire16-Aug-10 11:24 
GeneralRe: Um Pin
S.M. Zamshed Farhan11-Jan-13 5:52
memberS.M. Zamshed Farhan11-Jan-13 5:52 
GeneralMy vote of 1 Pin
dung_90ghost5-Apr-10 0:47
memberdung_90ghost5-Apr-10 0:47 
GeneralGood Tip Pin
Jon Anthony17-Dec-09 2:38
memberJon Anthony17-Dec-09 2:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151002.1 | Last Updated 23 Dec 2009
Article Copyright 2009 by koula7
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid