BrownChiLD
BrownChiLD

Reputation: 3703

Will the browser cache php include files? (PHP and templating)

I was just wondering if files loaded via php include is getting cached by the browser (my expected behaviour)

im trying to figure out a better way to designing my template engine..

i've always used php includes for header, footer, and etc.. for templating

but now considering going back to using dreamweaver templates w/c basically writes each web page w/ the full document code (inclusive of header, footer and all) and upon updating of the , say, header part, Dreamweaver rewrites all the pages that uses the said template.

I used to think this was a crude, noobish way to do templating, but now i'm realizing that' it's really pretty smart.. no more inlcudes and other processes, just plain good old page serving most of the required visual data. I "think" this is faster than apache having to run more threads for each include. and also that the pages are fully cached..

like visiting home.php the second time will result to minimal bandwidth costs.. vs php include, if NOT CACHED, has to send , for example the header.php data, again and again in the browser at every visit.

Also, DW templates actually make it easier for me to code/design, where php includes are often rendered in visiual views/tools

Just wondering...

PS As a backgrounder, i've actually used DW Templates w/ PHP templating (sort of a hybrid way of using DW) .. i've gotten really comfortable w/ this method and found it to be really straight forward.. hence im not really a stranger to either DW or PHP templating.

Upvotes: 0

Views: 2193

Answers (2)

akhilless
akhilless

Reputation: 3209

You know, it does not matter to the browser how the page is generated. You can use PHP, Python, Java, Ruby or other language - it just does not mater. The Apache server (in case of PHP) parses the script and returns the resulted HTML page to the browser. Its is cached by the browser by default - unless you use pragma <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">which indicates to the browser that the page should not be cached.

When you store and serve the page in the HTML rather than a script it just saves you the parsing stage and thus saves resources on the server and is generally faster. That is why most developed web frameworks include the ability to generate, store and serve a parsed page rather than a script "original". The typical scenario is check in PHP whether the page was modified. If it was modified, you regenerate the page; if not - serve the HTML one instead of the scripting one. You can also base this scenario on timeouts. It is especially useful in parts of you web application where the page generation requires fetching a lot of information from the database that does not frequently change. For example, on pages with long product or product category listings - update the page when a new product or product category is added respectively.

So "templating" (actually the serving the fully parsed version of the page instead of its scripted original) saves you server resources and page delivery times, not browser requests.

Upvotes: 2

deceze
deceze

Reputation: 522005

PHP's include has nothing to do with the browser, the browser doesn't know anything is being included on the server. All the browser gets to see is one long HTTP document, it doesn't matter how it was assembled on the server. The browser will cache it all the same.

What you want is to cache the document server side, so it doesn't have to be reassembled on each request.

         SERVER SIDE                   CLIENT

+-----------+     +--------+   |    +---------+
|           |     |        |   |    |         |
|    PHP    | --> | Apache | --|--> | browser |
|           |     |        |   |    |         |
+-----------+     +--------+   |    +---------+

      ^                ^                 ^
      |                |                 |
include happens     document         sees only
    here          already fully     complete HTML
                 assembled here   document regardless

Upvotes: 5

Related Questions