473,406 Members | 2,273 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,406 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 1757
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.