views:

50

answers:

4

what is the sense of writing these lines of codes

header("Cache-Control: no-cache, must-revalidate");
header("Expires: May, 17 May 1983 05:00:00 GMT\n");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

and where we have to write these lines of codes? on the page where we do databse dealing or just any php pages?

A: 

You should read this article about SEO and speed optimization.

The cache, which is local copies of resources, works because many resources change infrequently. When a browser can reuse a local copy, it saves the time to set up a connection as well as the time to download. The key to making the cache work effectively is HTTP caching headers, which are sent by the web server to specify how long a resource is valid and when it last changed.

If data on your page changes quickly (like stock exchange ticker) you should avoid cashing. But in most cases you should use it. You can also test your page using Yslow or page speed add-on for firebug.

nemke
+1  A: 

where we have to write these lines of codes? on the page where we do databse dealing or just any php pages?

On any page that will display data that you do not want cached because its content is changing quickly and constantly. If a page is not changing often you do not want to disable caching as it will cause a decrease in performance for your pages and an increased burden on your server. Caching is a good thing. Only disable it when necessary.

John Conde
A: 

The code you posted prevents clients from caching the HTTP response. You would include those lines if you want a fresh copy of your page downloaded from the server every time a user views it. This can be useful if the data on your page changes often. There is, of course, a bandwidth cost so you must think carefully about which pages on your site are cacheable and which are not.

Asaph
+1  A: 
header("Cache-Control: no-cache, must-revalidate");

to tell http/1.1 clients not to cahce the page

header("Expires: May, 17 May 1983 05:00:00 GMT\n");

to tell http/1.0 clients (which won't understand the previous line) not to cache the content

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

Its polite and useful for debugging to put this in - doesn't make any difference to how the page is treated for caching after the former two lines

and where we have to write these lines of codes? on the page where we do databse dealing or just any php pages?

You should never incorporate any code you don't understand in your product. Using caching properly is a good idea though and can save a lot of money. Have a google for Mark Nottinghams tutorial on cacheing for a starter.

C.

symcbean