Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

cache control no working

Posted on 2007-11-30
10
244 Views
Last Modified: 2008-02-01
i have the following lines of code below in my PHP page to control caching of my pages.  however I ran some tests to test cache control and im told i dont have any cache control setup...

I using Acunetix's Web Vulnerability Scanner.

Have I set my cache control incorrectly below?

$offset = 3600 * 24;
 
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Cache-Control: max-age=3600, must-revalidate');
header('Expires: '.gmdate('D, d M Y H:i:s',time() + $offset).' GMT');

Open in new window

0
Comment
Question by:ellandrd
  • 4
  • 4
  • 2
10 Comments
 
LVL 16

Expert Comment

by:grahamnonweiler
ID: 20381659
The following lines must be at the top of your scripts, either the first 3 lines or if you are using session control the first three lines immediately after your session start call. Additionally, it could be that your webserver is also sending cache control statements which are conflicting - however - the below statements are generic enough to handle such a situation.

header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

There is no real need to include a date last modified as the receiving browser/proxy could be misconfigured date wise and this would then cause conflicts. Best to simply prevent all caching regardless of date modified, which is what the above headers achieve.

Also remember PHP by default does not buffer output, so "header" must be used as early as possible in any of your scripts and ideally the first few lines.
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20381673
ellandrd,

what is your goal?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 20381737
wait 2 secs - i want to cache my pages not, not cache them like your lines of code do..
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 16

Expert Comment

by:grahamnonweiler
ID: 20381835
So you want to reload the page after 2 seconds?

Remember that images and other static data such as Javascript and CSS is always cached by the browser and can not be controlled by server side scripts.
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20381877
Try put this (html) at the top of your page (either echo or put in your html section of the code) to disable caching:
<META HTTP-EQUIV ="Expire" CONTENT ="0">

and to reload page after 2 seconds use this code:

<?php
$redirector = "<meta HTTP-EQUIV=\"refresh\" content=2;url=\"YOUR_PAGE.php\">";
echo $redirector;
?>

change YOUR_PAGE.php to whatever your page is called (filename).

Hope this helps.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 20382198
>>So you want to reload the page after 2 seconds?

No I dont want to reload or refresh my page every 2 seconds.

I want to cache my page and any elements within it for X amount of time.  when that time is up, re-validate page...
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20386438
<?php
$redirector = "<meta HTTP-EQUIV=\"refresh\" content=2;url=\"YOUR_PAGE.php\">";
echo $redirector;
?>
This will refresh every 2 seconds, provided your YOUR_PAGE.php is replaced with the actual script name.

To set the cache to expire at a certain time, something like this:
<meta http-equiv="Expires" content="date">

and replace 'date' with whatever you want to expiry date to be, EG:
<META HTTP-EQUIV="expires" CONTENT="Wed, 09 Dec 2008 08:21:57 GMT">
0
 
LVL 16

Author Comment

by:ellandrd
ID: 20403304
i want to cache my pages and set them to expire after X 3600 minutes.  all of the above so far force not to cache or fresh the page stupidly
0
 
LVL 21

Accepted Solution

by:
nizsmo earned 500 total points
ID: 20406011
Sorry i misread you wanted to refresh your page every 2 seconds.

however the meta tag for cache expiring should work, combined with some php code to set the date of expiry to what you want (after 3600 minutes).

More on the expire metatag here:
http://www.i18nguy.com/markup/metatags.html
0
 
LVL 16

Author Comment

by:ellandrd
ID: 20409626
nizsmo

isnt the code i have above do this?  i want to know if it is all correct - please reread my first comments again
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question