4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 PHP and caching - how to make cacheable pages
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
kjellkk
Junior WebHelper
Junior WebHelper


Joined: 06 Mar 2002
Posts: 1

PostPosted: Thu Mar 07, 2002 4:01 pm (15 years, 7 months ago) Reply with QuoteBack to Top

Hi,

Long time no talk - first of all just congrats and thanks to the team for setting up 4webhelp to continue the good work started as www.users.f2s.com.

So to my question/information. It was long time ago after frequenting the f2s and users.f2s forums that I ended up using PHP to apply my "templates" through php include. Basically just pulling in one file on top and one file at the bottom to put in standard menus or other elements.
At that time few of the f2s accounts supported SSI so the general go was that "PHP can do what you want and much more" which is definiately true.

But what I've now understood is that one important difference, if you mostly use PHP to put up templates on regular pages and do not programming within them, is that your pages are NOT at all cacheable for the user. Meaning that a user with expensive dialup connection (many of mine are) cannot just quickly browse through the pages he needs to read and then go offline to read. That would only work for the page he views when disconnecting, any attempt to view other pages will require connection.

So, pretty bad since I configured my server with:

AddType application/x-httpd-php .php3 .php .html

Meaning close to all my pages are no longer cacheable... that is except for the CGI scripts (!) and it would seem SSI pages.

So - what's a man to do? When most of his visitors need to have some cacheability? Checked various places on the internet, but most treat the issue of "how to avoid caching of PHP pages" ... I found some going a bit beyond my needs and makin results of dbase queries cachable, to reduce server load. Not exactly the issue here - but in the end some results after consulting the PHP manual and comments on the internet. Thought I'd share them here and ask for inputs on other ways to do this.

The code below should be placed in a separate file to be included on the pages you want to make cacheable (has to be before :any HTML code):

Code:
<?php
define('HTTP_DATE_FORMAT','%a, %d %b %Y %R GMT'); // Defines the date format to be HTTP standard
$later=time()+908000; // x hours later // Defines the time before the page expires
$lastmod=filemtime($PATH_TRANSLATED); // Defines the date the file was last changed - NB! Read cmt.
header('Expires: '.gmstrftime(HTTP_DATE_FORMAT,$later)); // Outputs the value for when the doc expires
header('Cache-Control: max-age=604800'); // Outbuts the value for the cache-control, amount in secs
header('Last-Modified: '.gmstrftime(HTTP_DATE_FORMAT,$lastmod)); // Outputs the time for Last-Modified
?>


The definition of $lastmod will only work if you have this in a separate file and call it. If you want to put this directly in a file you should probably replace $PATH_TRANSLATED with $current_file .

Now my question is if there's a much simpler way to work this, and have Apache serve all my PHP pages as cacheable. And instead of including this on all my pages I'd include the opposite on those pages that shouldn't be cacheable... ???

Ok some resources to check out to play with the settings of the above code (to set caching to your preferences):

http://www.web-caching.com/ (Cache tutorial + check your pages)
http://httpd.apache.org/docs/mod/mod_expires.html

Inputs/comments/revisions welcome,

-kjell

________________________________
==
Kjell Knudsen | http://www.icbl.org
OfflineView User's ProfileFind all posts by kjellkkSend Personal MessageVisit Poster's Website
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




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.
You cannot vote in polls in this forum.


Page generation time: 0.048296 seconds :: 17 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme