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?
LVL 12
j2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

j2Author Commented:
Edited text of question.
0
j2Author Commented:
Edited text of question.
0
gravityCommented:
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.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

j2Author Commented:
so, i echo that before my <html> or? Uhm, im lost :)
0
gravityCommented:
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.
0
j2Author Commented:
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)
0
gravityCommented:
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
0
j2Author Commented:
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');
0
gravityCommented:
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 :)
0
gravityCommented:
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.
0
j2Author Commented:
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.
0
j2Author Commented:
ok, ill try it tomorrow. thanks for now!
0
gravityCommented:
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.
0
j2Author Commented:
welll, the "anti-back" button works in phpMyAdmin (for instance) (and yes, my browser is set to reload on every visit)
0
gravityCommented:
"anti-back" button ? Hmm, it's been a while since I last used phpmyadmin, but when you click on the back button, what happens ?
0
j2Author Commented:
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? :)
0
j2Author Commented:
(and that page is not gerver side generated, its a 'local' explorer message)
0
gravityCommented:
Is that message taken straight from the browser ? The same thing should happen if you go to any page with a form, enter data into it and submit, then go to a new page, and then click back.
I still remain adamant that whilst you can muck around with the back button using javascript (a practise which I'm against - at the end of the day, it's *my* browser :) there is no way to make the browser refresh that back button data.
This probably comes down to the browser and how it's cache is used... after all, a user can always load up a page from their cache which is old, and then muck around with it before using it to send data to your new page.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
j2Author Commented:
You're right, "Thats all one can do" so to speak.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.