最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Setting no-cache in html headers doesn't work - Stack Overflow

matteradmin8PV0评论

I want to prevent the user from pressing going to the previous page (back button in browser), after logout.

I manage to do this in apache adding this to the configuration:

<FilesMatch "\.(html|htm|js|css|pl)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</FilesMatch>

But when i do it directly in the sources it doesnt work i'm using:

<meta content="no-cache" http-equiv="Pragma"></meta>
<meta content="no-cache, no-store, must-revalidate" http-equiv="Cache-Control"></meta>
<meta content="0" http-equiv="Expires"></meta>

I want to prevent the user from pressing going to the previous page (back button in browser), after logout.

I manage to do this in apache adding this to the configuration:

<FilesMatch "\.(html|htm|js|css|pl)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</FilesMatch>

But when i do it directly in the sources it doesnt work i'm using:

<meta content="no-cache" http-equiv="Pragma"></meta>
<meta content="no-cache, no-store, must-revalidate" http-equiv="Cache-Control"></meta>
<meta content="0" http-equiv="Expires"></meta>
Share Improve this question edited Feb 12, 2014 at 9:14 Carlos Escalera Alonso asked Feb 12, 2014 at 8:32 Carlos Escalera AlonsoCarlos Escalera Alonso 2,3633 gold badges27 silver badges37 bronze badges 8
  • You have discovered that HTTP headers can set caching options and that HTML meta elements pretending to be equivalent … aren't equivalent. What's the question? – Quentin Commented Feb 12, 2014 at 9:23
  • How can I prevent the page to cache so the user cant go back after logout. I make it work in apache conf, but when I tried to add it directly using the example from here Making sure a web page is not cached, across all browsers, doesnt work. What I'm missing? – Carlos Escalera Alonso Commented Feb 12, 2014 at 9:48
  • "I make it work in apache conf" ← like that. – Quentin Commented Feb 12, 2014 at 9:50
  • Yes setting it in apache configuration works well, but dont know how to do it from the source – Carlos Escalera Alonso Commented Feb 12, 2014 at 10:06
  • From the HTML source? Are you have already discovered, you cannot. – Quentin Commented Feb 12, 2014 at 10:07
 |  Show 3 more ments

1 Answer 1

Reset to default 2

As Quentin exlplain this meta tags are ignored by the browser.

<meta content="no-cache" http-equiv="Pragma"></meta>
<meta content="no-cache, no-store, must-revalidate" http-equiv="Cache-Control"></meta>
<meta content="0" http-equiv="Expires"></meta>

So this is what worked for me to disable the cache in all browsers.

From perl

 Use CGI;

 sub set_new_query() {
            $query = CGI->new();
            print $query->header(
                            -expires       => 'Sat, 26 Jul 1997 05:00:00 GMT',
                            -Pragma        => 'no-cache',
                            -Cache_Control => join(', ', qw(
                                                private
                                                no-cache
                                                no-store
                                                must-revalidate
                                                max-age=0
                                                pre-check=0
                                                post-check=0 
                                               ))
            );
        }

An alternative in Apache is to add to httpd.conf:

LoadModule headers_module modules/mod_headers.so

<FilesMatch "\.(html|htm|js|css|pl)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
</FilesMatch>

And for other language here is a great description: Making sure a web page is not cached, across all browsers

Post a comment

comment list (0)

  1. No comments so far