473,223 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,223 software developers and data experts.

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

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.
Aug 18 '05 #1
5 1748
News KF wrote:
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.

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

Aug 19 '05 #2
On 18/08/2005 23:15, News KF wrote:
I'm new to cahce control, [...]
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]
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.


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.
Aug 19 '05 #3
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:

Also, I wonder why you use PHP instead of plain HTML, if you do not want to
change the content.
News KF wrote:
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.
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

Good luck and regards,
Erwin Moller

Aug 20 '05 #4
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:
On 18/08/2005 23:15, News KF wrote:
I'm new to cahce control, [...]

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]
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.

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

Aug 20 '05 #5
*** News KF wrote/escribió (Fri, 19 Aug 2005 00:15:46 +0200):
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.


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
--
Aug 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steven | last post by:
Hi there, I have a database with documents that have event dates included. What I need to do is have the Expires META tag to be one day after that event date. Can I get the date from the...
3
by: techie | last post by:
I am using the following to delete the contents of a cookie Response.Cookies("maincookie").expires = DateAdd("d",-2,now) Response.redirect "login.asp" If i try to alert the value of the cookie...
5
by: George Hester | last post by:
function spawn(ev){ var oSelect = ''; var oFlag = false; if (ns4 || ns6) oSelect = document.getSelection(); else if (ie4 || ie5){ if (!ev) ev = window.event; oFlag = true; oSelect =...
3
by: InvisibleMan | last post by:
Thanks in Advance for any help on this - its truely sending my head in loops... so I appreciate your efforts! okay, I have a javascript listed below that drops down submenus contained within:...
8
by: John Dalberg | last post by:
What happens when a cookie expires? Does it mean that when the browser or sessions ends, it doesn't get saved? I am using Opera and looking at available cookies and I can some cookies that have...
16
by: Konrad Viltersten | last post by:
Suppose you got a really long page and you'd like to enable the user (supposedly, there's only one but if it's not to difficult we could extend that to any number) not to have to scroll to the...
12
by: ACaunter | last post by:
Hi all, I was wondering how i could write some code which would automatically open the Login Page once the session has expired? -- AdamPC@hotmail.com
1
by: Agent Michael Scarn | last post by:
Hello All, After I submit a form it goes to a post page where I have a javascript that creates one variable that has all of the element names from the form listed out like so: ...
6
by: Michi Henning | last post by:
I'm running the following code in Safari 2.0.4: document.cookie = 'MyCookie=1'; if(document.cookie == '') alert('document.cookie is empty!'); document.cookie always returns the empty string,...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.