Link to home
Start Free TrialLog in
Avatar of j2
j2Flag for Sweden

asked on

cache invalidation

Shouldnt the below code invalidate the cache for a page? This is a part from my commin.inc, and it is called for the pages for which i do not want any caching, but alas, it doesnt work :)

function nocache_header($title) {
          echo "<html><head><title>\n";
        echo "$title\n";
        echo "</title>\n";
      echo "<link rel=stylesheet href=\"http://localhost/cs.css\" type=\"text/css\" title=\"style sheet\">\n";
      
      echo "<META HTTP-EQUIV=\"Expires\" CONTENT=\"Fri, Jun 12 1981 08:20:00 GMT\">\n";
      echo "<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">\n";
      echo "<META HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-cache\">\n";
     
        echo "</head>\n";
}

Or rather "How do i design a header that tells the browser to invalidate a page?" I need this to (to a certain degree) stop double posting when someone uses the "back" button on their browser?
Avatar of j2
j2
Flag of Sweden image

ASKER

Edited text of question.
Avatar of j2

ASKER

Edited text of question.
Avatar of gravity
gravity

Basically, you've got it all right, but you need to replace the Meta tags with Headers (which must be put before any output is sent blah blah blah).
e.g.

header("Cache-Control: no-cache, must-revalidate");          

header("Expires: Wed, 18 Jan 2000 07:30:00 GMT");            

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

The last one will generate a header that outputs the current time, date and the like, thus always telling the browser to go for a new version beacause the current page it has is old.
Avatar of j2

ASKER

so, i echo that before my <html> or? Uhm, im lost :)
Basically, yes...

Any Header() function data is sent before anything else that the browser receives. It tells the browser such infomation as what file is being sent (I think :) and what language it is in.
Therefore, headers must be sent to the browser first thing before any other data is sent.
So, in order to use them you can put them wherever you like in the code as long as *nothing* is sent to the browser (eg HTML) before they are... that includes any spaces before the "<?" tag.
You don't need to echo them... just call them on their own and everything should be groovy.
Avatar of j2

ASKER

now i use

<?php
function nocache_header($title) {
      header("Cache-Control: no-cache, must-revalidate");            
      header("Expires: Wed, 18 Jan 2000 07:30:00 GMT");              
      header("Pragma: no-cache");                                    
      header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
          echo "<html><head><title>\n";
        echo "$title\n";
        echo "</title>\n";
      echo "<link rel=stylesheet href=\"http://www.localhost.org/cs.css\" type=\"text/css\" title=\"style sheet\">\n";
        echo "</head>\n";
      echo "<body background=\"http://www.localhost/images/cs-logo-fill2.jpg\" topmargin=\"0\" leftmargin=\"0\"style=\"font-family: Arial, sans-serif\" bgcolor=\"#B07879\">\n";
  }

function meta_footer() {
    echo '</body></html>';
  }

(lots of other functs here)

But i still get the

Warning: Cannot add more header information - the header was already sent (header information may be added only before any output is generated from the script - check for text or whitespace outside PHP tags, or calls to functions that output text) in ../common.inc on line 3

any clues?

(i upped the points, since this wasnt as straightforward as i thought)
That all seems pretty good... are you sure that there is no whitespace before any of the <?php tags ?
As long as you're not calling the function after some other one which writes to the browser then you should be fine.
Check the HTML source... if there's anything before the <HTML> tag then something's up
Avatar of j2

ASKER

all before the html (in fact anything before and including body) is in my common inc

all i have in the calling script is

include('common.inc');
nocache_header('pagename');
One filal check... is there any whitespace at the bottom of the common.inc file, outside of the ?> tag ?

Just asking while I test your code :)
Update : On their own, I have no problems running the "nocache_header()" or "meta_footer()" functions when they're on their own inside of "<? ?>" tags, without the white space, obviously.
Therefore one of the include scripts must have space in it...
Try putting the above mentioned functions in a separate include file and then including that for the time being, just to see if that works.
If it does then it's confirmation of the problem lying in the other include file.
Avatar of j2

ASKER

Found a NULL char there :)

So now the code executes, but i still dont get the "Page expired" when using my back button, and i do not "see" the headers in the source of my page.
Avatar of j2

ASKER

ok, ill try it tomorrow. thanks for now!
Ahh, the back button (I think certainly in IE) will never reload a page... ever terminated your connection and then gone back thru the pages you've visited ? Unless the person is cheating by getting the page to refresh itself every one in a while using a META tag then the browser will always cache it. Sad fact of life.
However, each time the user clicks on a link to the page with the above headers, the browser *should* get a fresh version of the page, unless the user has set it up not to... fact of life, you can't control users :)
As for the headers not appearing... this was what I was trying to get at.
They're sent by the server to the browser to tell it what kind of file and the like it's dealing with.
File extensions mean nothing in the world, so how would one browser know to display a zip file or not ? Headers is the answer.
Avatar of j2

ASKER

welll, the "anti-back" button works in phpMyAdmin (for instance) (and yes, my browser is set to reload on every visit)
"anti-back" button ? Hmm, it's been a while since I last used phpmyadmin, but when you click on the back button, what happens ?
Avatar of j2

ASKER

Ok, lets say i run a sql query, (or similar) and then execute it. When i see the status page. And click "back" i get a

"Page has expired. You can not view this page without resubmitting the form data"  (translated from swedish)

Btw, is it just me, or is the PHP topic missing from the new layout? :)
Avatar of j2

ASKER

(and that page is not gerver side generated, its a 'local' explorer message)
ASKER CERTIFIED SOLUTION
Avatar of gravity
gravity

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of j2

ASKER

You're right, "Thats all one can do" so to speak.

Thanks.