473,774 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<$ma x;$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 1778
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<$ma x;$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($_SER VER['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
3366
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 appropriate field in the database and format it into GMT to add into the header() function? Cheers
3
2328
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 in the same page, it shows the old value. As well as when it is alerted from the login.asp it shows the old value. The problem is when i try to update the value of the cookie in the login page, it does not update the new value.
5
4209
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 = document.selection;
3
2448
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: <div class="small" style="display: none" id="menu1_menu"> when the heading is clicked: <a href=.. onClick="expandDiv('menu1')">
8
14023
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 expiration dates in the past. Does this mean that they are in memory and they are still valid cookies which sites can test for their existance and return true. I thought if a cookie's expiration is set in the past, it gets removed from memory and...
16
1618
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 last position in the document he/she was viewing but simply auto-jump him/her to it. Is that doable at all using JS? I guess it would be somewhere in the vicinity of: - remember every scroll - save the line number to the users HDD
12
6995
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
2489
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: var1=var1&var2=var2&var3=var3 and so on...
6
21025
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, no matter what I do. I checked the cookie settings in Safari->Preferences->Security. Even when I set cookies to be always accepted, document.cookie still returns the empty string.
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.