GPWiki.org
GPWiki.org
It is currently Sun May 19, 2013 12:57 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Oct 02, 2012 10:11 am 
Rookie

Joined: Sat Sep 29, 2012 4:11 am
Posts: 2
PHP - How to cache a page use php?. Thanks for idea!

_________________
Life is fun. I watch "Game of thrones" & funny friv flash


Top
 Profile  
 
PostPosted: Tue Oct 02, 2012 11:13 pm 
Prolific Poster

Joined: Tue Sep 25, 2012 12:57 pm
Posts: 15
Location: Adelaide, Australia
You will need to do a few things.

Firstly, you need to set your expires and cache-control headers. Depending on the browsers you want to support, you can skip the Expires header. You will probably want to take a read of the HTTP spec to understand all of the options available in Cache-Control
Code:
$max_age = 60 * 60 * 24;    // 24 hours

header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $max_age) . ' GMT');
header('Cache-Control: cache, store, max-age=' . $expires . ', must-revalidate');


Secondly, you will need to support one or both of ETag/If-None-Match or Last-Modified/If-Modified-Since. These are headers which allow for cache revalidation. You don't need to support these, but you need to support them if you want to be able to expire early. If you're not supporting these, ditch the must-revalidate clause in Cache-Control.

Here is a function I use to set (and check) the etag, as well as setting the correct headers:
Code:
function etag($etag, $max_age) {
   header('ETag: "' . $etag . '"');
   header('Pragma: public');
   header('Cache-Control: store, cache, must-revalidate, max-age=' . $max_age);
   header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $max_age) . ' GMT');
   
   if ($_SERVER['HTTP_IF_NONE_MATCH']) {
      $match = str_replace('"', '', $_SERVER['HTTP_IF_NONE_MATCH']);
      if ($match == $etag) {
         header('HTTP/1.0 304 Not Modified');
         exit;
      }
   }
}


You then call it like this:
Code:
$etag = md5($something_unique);
$max_age = 60 * 60 * 24;
etag($etag, $max_age);

echo 'This should be cached for 24 hours';


The variable $something_unique needs to be unique for each version of the page. If the page content changes, this variable needs to change too (or the etag won't work). Likewise, if the content doesn't change, the etag should remain the same. If you were doing a revision-based cms, you could use the page id and revision id as the unique key. When a new revision goes live, the revision id would change, and the etag would likewise change.

Don't forget to take a look at RedBot (http://redbot.org/) which will help a lot.

_________________
Chaotic Rage - a fast paced, shooter game that's a little odd, but still fun.


Top
 Profile  
 
PostPosted: Mon Oct 08, 2012 7:00 pm 
Babirusa
User avatar

Joined: Thu Aug 19, 2004 2:55 pm
Posts: 9241
Location: The Netherlands
Also make sure to use tools like APC (advanced PHP cache) or Memcache. They will significantly increase the performance of your PHP files, as no compilation is required at request time.

It does require the ability to install an extension on the server.

_________________
Serious game developer

http://www.persistentrealities.com
http://www.persistentrealities.com/vbfibre
http://www.ambiances.nl


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group