Connecting Tech Pros Worldwide Forums | Help | Site Map

how to mark php document as cachable (expires only when php filechanges)

News KF
Guest
 
Posts: n/a
#1: Aug 18 '05
Hi,


I'm new to cahce control, so I hope my question makes sense.


let's assume a small php file
like
<html><body>
<?php
$max=10;
for($i=0;$i<$max;$i++){
print("$i<br>\n");
}
?>
</body></html>


Obviously the output of this file is predictably constant as it would be
for any html file.

I assume, that apache would allow a foreign browser to detect, that
a html file (creation date or expire) does not have to be reloaded
except it has been updated on the server.


On the other hand I assume, that php will set the expire attributes for
php files per default such, that the request will not be cachable, as
php is normally being used for dynamic web pages.

However my above php file will always create the same output independent
of when and how it will be called.


So what exact header would I have to pass to indicate to make the
browser understand, that the php file does not have to be refetched,
except the php file (its creation date) changed.



Thanks in advance for any support.


If my request is not clear, then I'll reexplain differently.

Erwin Moller
Guest
 
Posts: n/a
#2: Aug 19 '05

re: how to mark php document as cachable (expires only when php filechanges)


News KF wrote:
[color=blue]
> Hi,
>
>
> I'm new to cahce control, so I hope my question makes sense.
>
>
> let's assume a small php file
> like
> <html><body>
> <?php
> $max=10;
> for($i=0;$i<$max;$i++){
> print("$i<br>\n");
> }
> ?>
> </body></html>
>
>
> Obviously the output of this file is predictably constant as it would be
> for any html file.
>
> I assume, that apache would allow a foreign browser to detect, that
> a html file (creation date or expire) does not have to be reloaded
> except it has been updated on the server.
>
>
> On the other hand I assume, that php will set the expire attributes for
> php files per default such, that the request will not be cachable, as
> php is normally being used for dynamic web pages.
>
> However my above php file will always create the same output independent
> of when and how it will be called.
>
>
> So what exact header would I have to pass to indicate to make the
> browser understand, that the php file does not have to be refetched,
> except the php file (its creation date) changed.
>
>
>
> Thanks in advance for any support.
>
>
> If my request is not clear, then I'll reexplain differently.[/color]


Hi,

Your question is clear.
However, apache is maybe not the easiest place to start caching
PHP-documents.
I think you should tell the browser to cache it.
But caching can be very confusing, and results may differ from one setup to
the next (depending on browser, version, proxies, OS, preferences in the
browser, etc etc.).
So be sure you try the solution on a few different setups.

But read up here, it contains a lot of headers and some explanations on how
to use them:
http://nl2.php.net/header

Also, I wonder why you use PHP instead of plain HTML, if you do not want to
change the content.

Good luck and regards,
Erwin Moller

Michael Winter
Guest
 
Posts: n/a
#3: Aug 19 '05

re: how to mark php document as cachable (expires only when php filechanges)


On 18/08/2005 23:15, News KF wrote:
[color=blue]
> I'm new to cahce control, [...][/color]

The caching tutorial[1] by Mark Nottingham is well worth a read, but I
strongly suggest that you read RFC 2616, especially section 13, for more
detailed information. You will also want to read about the various
headers that can be involved in caching: Cache-Control (14.9), ETag
(14.19), Expires (14.21), conditional requests (14.24-6,8), and
Last-Modified (14.29).

[snip]
[color=blue]
> So what exact header would I have to pass to indicate to make the
> browser understand, that the php file does not have to be refetched,
> except the php file (its creation date) changed.[/color]

You could send the Last-Modified header with a value reflecting the last
modified time of the PHP file:

header('Last-Modified: '
. gmdate('D, d M Y H:i:s', getlastmod())
. ' GMT');

Mike


[1] <URL:http://www.mnot.net/cache_docs/>

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
News KF
Guest
 
Posts: n/a
#4: Aug 20 '05

re: how to mark php document as cachable (expires only when php filechanges)


Hi Erwin,

Thanks,

I look at this info.


The reason I'd like to use php is similiar to my example (a for loop
prints out multiple lines of html.

In my real example the php code is much shorter than the created html
and it will be easier to maintain.


Erwin Moller wrote:[color=blue]
>
> Also, I wonder why you use PHP instead of plain HTML, if you do not want to
> change the content.
>
>
> News KF wrote:[color=green]
>>Hi,
>>
>>
>>I'm new to cahce control, so I hope my question makes sense.
>>
>>
>>let's assume a small php file
>>like
>><html><body>
>><?php
>>$max=10;
>>for($i=0;$i<$max;$i++){
>> print("$i<br>\n");
>>}
>>?>
>></body></html>
>>
>>
>>Obviously the output of this file is predictably constant as it would be
>>for any html file.
>>
>>I assume, that apache would allow a foreign browser to detect, that
>>a html file (creation date or expire) does not have to be reloaded
>>except it has been updated on the server.
>>
>>
>>On the other hand I assume, that php will set the expire attributes for
>>php files per default such, that the request will not be cachable, as
>>php is normally being used for dynamic web pages.
>>
>>However my above php file will always create the same output independent
>>of when and how it will be called.
>>
>>
>>So what exact header would I have to pass to indicate to make the
>>browser understand, that the php file does not have to be refetched,
>>except the php file (its creation date) changed.
>>
>>
>>
>>Thanks in advance for any support.
>>
>>
>>If my request is not clear, then I'll reexplain differently.[/color]
>
>
>
> Hi,
>
> Your question is clear.
> However, apache is maybe not the easiest place to start caching
> PHP-documents.
> I think you should tell the browser to cache it.
> But caching can be very confusing, and results may differ from one setup to
> the next (depending on browser, version, proxies, OS, preferences in the
> browser, etc etc.).
> So be sure you try the solution on a few different setups.
>
> But read up here, it contains a lot of headers and some explanations on how
> to use them:
> http://nl2.php.net/header[/color]
[color=blue]
> Good luck and regards,
> Erwin Moller
>[/color]
News KF
Guest
 
Posts: n/a
#5: Aug 20 '05

re: how to mark php document as cachable (expires only when php filechanges)


Hi Mike,


I'll dig into your details, but on a first glance your threeliner is
exactly what I imagined as solution.


bye



nkf


Michael Winter wrote:[color=blue]
> On 18/08/2005 23:15, News KF wrote:
>[color=green]
>> I'm new to cahce control, [...][/color]
>
>
> The caching tutorial[1] by Mark Nottingham is well worth a read, but I
> strongly suggest that you read RFC 2616, especially section 13, for more
> detailed information. You will also want to read about the various
> headers that can be involved in caching: Cache-Control (14.9), ETag
> (14.19), Expires (14.21), conditional requests (14.24-6,8), and
> Last-Modified (14.29).
>
> [snip]
>[color=green]
>> So what exact header would I have to pass to indicate to make the
>> browser understand, that the php file does not have to be refetched,
>> except the php file (its creation date) changed.[/color]
>
>
> You could send the Last-Modified header with a value reflecting the last
> modified time of the PHP file:
>
> header('Last-Modified: '
> . gmdate('D, d M Y H:i:s', getlastmod())
> . ' GMT');
>
> Mike
>
>
> [1] <URL:http://www.mnot.net/cache_docs/>
>[/color]
Alvaro G Vicario
Guest
 
Posts: n/a
#6: Aug 23 '05

re: how to mark php document as cachable (expires only when php filechanges)


*** News KF wrote/escribió (Fri, 19 Aug 2005 00:15:46 +0200):[color=blue]
> So what exact header would I have to pass to indicate to make the
> browser understand, that the php file does not have to be refetched,
> except the php file (its creation date) changed.[/color]

I use the following code:

header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($_SERVER['DOCUMENT_ROOT'] . $_SERVER['PHP_SELF'])) . ' GMT');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT');

Set time() + 86400 to different values if you want different expiration dates.

--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Closed Thread