Jump to content

How to cache a .php page that is included on another page


romeoyankee

Recommended Posts

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?

 

 

 

 

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;
?>

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!

 

 

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?

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
    ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.