hello,

I am trying to experiment with page caching. I would like my page to be cached by the browser for 15 mins. Because the page content does not change much but it downloads thousands of the same records from the database every time the page loads.

I am generating the following header:

<meta http-equiv="Expires"  content="Mon, 21 Sep 09 14:26:47 +0000" />
<meta http-equiv="Cache-control" content="private" />

with the following php code:

gmdate(DATE_RFC822, (mktime() + 900));

I am also printing out the the current time on the page to check if the page is really being cached or not.

echo date("H:i:s");

It is my understanding that the time would not update on the web page if the page is really being cached.
Unfortunately it's not.

Kindly help. Any advice would be appreciated.

Recommended Answers

All 5 Replies

hello,

I am trying to experiment with page caching. I would like my page to be cached by the browser for 15 mins. Because the page content does not change much but it downloads thousands of the same records from the database every time the page loads.

I am generating the following header:

<meta http-equiv="Expires"  content="Mon, 21 Sep 09 14:26:47 +0000" />
<meta http-equiv="Cache-control" content="private" />

with the following php code:

gmdate(DATE_RFC822, (mktime() + 900));

I am also printing out the the current time on the page to check if the page is really being cached or not.

echo date("H:i:s");

It is my understanding that the time would not update on the web page if the page is really being cached.
Unfortunately it's not.

Kindly help. Any advice would be appreciated.

Try sending them as HTTP headers instead of HTML tags.

Example:

header('Cache-control: private');
header('Expires: '. gmdate(DATE_RFC822, (mktime() + 900)) );

hello,
Doing that generated generated a header error. It said that header already sent. I think I am not allowed to a use a php header and a html header.

hello,
Doing that generated generated a header error. It said that header already sent. I think I am not allowed to a use a php header and a html header.

You can buffer the output of your PHP script using the PHP output buffering functions.
http://www.php.net/manual/en/book.outcontrol.php

At the top of your PHP script, place:

ob_start();

http://www.php.net/manual/en/function.ob-start.php

You should be able to send the headers then.

Thank you. It is now working when I press the back button.
But it does not work when I click on a link in the navigation.
Would you know why that is?
Many thanks in advance.

Thank you. It is now working when I press the back button.
But it does not work when I click on a link in the navigation.
Would you know why that is?
Many thanks in advance.

What browser are you using.

I get a the timezone difference at the end of the date when using DATE_RFC822. Maybe the browser does not like that.

eg:

Wed, 23 Sep 09 04:47:19 +0000

Maybe using this would work:

gmdate('D, d M Y H:i:s').' GMT'

This would be equivalent to the example at:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.21

And documented at:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1

Even though RFC 822 does define that the time difference can be used at the end of the date, you never know if a browser will accept it.
http://www.ietf.org/rfc/rfc0822.txt

See if that does it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.