romeoyankee Posted June 13, 2008 Share Posted June 13, 2008 My php knowledge is basically from just reading and following directions that other people have included in their php files. I have read the various posts in this forum on how to cache a page but without knowing exactly what I am doing its hard to figure out what I should do.. Basically my problem is this. I use rss2html to create a php page with an rss feed http://www.cyow.ca/template_news.php and then I use a php include to add that .php news template file onto my main index page. http://www.cyow.ca The problem is that this website is just for fun and I dont want to pay to get a solution like feedforalls. What I was hoping that I could do would be to cache the template_news.php file on my server and only have it updated every half hour or something like that. I figure that would help me lower the index.html load times. Is that at all possible? Link to comment https://forums.phpfreaks.com/topic/110078-how-to-cache-a-php-page-that-is-included-on-another-page/ Share on other sites More sharing options...
haku Posted June 13, 2008 Share Posted June 13, 2008 Yep. This tutorial explains how: http://www.developertutorials.com/tutorials/php/php-caching/page1.html Link to comment https://forums.phpfreaks.com/topic/110078-how-to-cache-a-php-page-that-is-included-on-another-page/#findComment-564889 Share on other sites More sharing options...
rhodesa Posted June 13, 2008 Share Posted June 13, 2008 tutorial looks very similar to this...but i already had it typed out...so here it is: sure, have your PHP script write the contents to a file. then you can check the mtime on the file to see if it needs updates: <?php $cache_file = 'template_news_cache.php'; $expires = 20 * 60; //20 minutes => seconds if(file_exists($cache_file) && filemtime($cache_file) + $expires > time()){ //load from cache echo file_get_contents($cache_file); } //Build the contents, and store it into $contents; //YOUR CODE HERE //Done building file_put_contents($cache_file,$contents); echo $contents; ?> Link to comment https://forums.phpfreaks.com/topic/110078-how-to-cache-a-php-page-that-is-included-on-another-page/#findComment-564898 Share on other sites More sharing options...
romeoyankee Posted June 13, 2008 Author Share Posted June 13, 2008 Thanks guys, So rhodesa ill start with yours because its there. Basically I create a new template_news.php file with the code you provided only.. and in the orange "build the contents... " I would put the main code that I want to use (basically what was already there)? then on the index.html page i include template_news.php and it knows to grab the cached file or if it is expired a new one? thanks again guys! Link to comment https://forums.phpfreaks.com/topic/110078-how-to-cache-a-php-page-that-is-included-on-another-page/#findComment-565042 Share on other sites More sharing options...
romeoyankee Posted June 13, 2008 Author Share Posted June 13, 2008 well it sorta worked... I couldnt get the example rhodesa posted to go but I used the example from the link kaku posted and I got it halfway to work. It doesnt seem to want to let me use multiple php includes in the script... Am I limited in the amount of php includes I can include? Link to comment https://forums.phpfreaks.com/topic/110078-how-to-cache-a-php-page-that-is-included-on-another-page/#findComment-565066 Share on other sites More sharing options...
romeoyankee Posted June 13, 2008 Author Share Posted June 13, 2008 ok I was doing something wrong.. It seems to be working now and it increased the speed of my site instantly!!!!!!!!!! Thanks a lot guys!! (I did end up using the code from the tutorial because I couldnt get the one rhodesa posted to work..) here is the code I used... <?php $cachefile = "cache/".$reqfilename.".html"; $cachetime = 5 * 60; // 5 minutes // Serve from the cache if it is younger than $cachetime if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) { include($cachefile); echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->\n"; exit; } ob_start(); // start the output buffer ?> .. Your usual PHP script and HTML here ... <?php $fp = fopen($cachefile, 'w'); // open the cache file for writing fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file fclose($fp); // close the file ob_end_flush(); // Send the output to the browser ?> Link to comment https://forums.phpfreaks.com/topic/110078-how-to-cache-a-php-page-that-is-included-on-another-page/#findComment-565085 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.