Skip to content
Advertisement

How to Turn off Caching of PHP Pages on Server and Browser?

I’m not sure where the caching of php pages is coming from. So far I’ve disabled caching in;

Chrome’s Developer Tools> Network > Disable cache;

Debian’s optphp73etcphp73.ini with;

 opcache.enable=0
 opcache.enable_cli=0

I’ve added to the end of my server entry;

fastcgi_cache_bypass $is_args;
  fastcgi_no_cache $is_args;

I’ve got this header at the top of the php script;

 header("Cache-Control: no-cache");

But I still can’t figure out Where the caching is coming from.

Can someone suggest other places I might look?

Advertisement

Answer

If you’re trying to stop the caching of your web pages using PHP I use the following code

//Minimize caching so admin area always displays latest statistics
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

While nothing is really full proof as many CSS and other JS files are cached independant of the PHP and require a refresh to display once updated there are other ways around this as well.

One example I use is to do a hash of the file (using php):

$specific_page = '/assets/javascripts/pages/javascript.js';
if(file_exists($specific_page)) {
    $html .= '<script src="'.$specific_page.'?v='.hash_file('crc32',$specific_page).'"></script>';
}

This will keep the cached file until the file is changed. When the file is changed it changes the hash and thus will find the new updated file (as the URL has technically changed).

Again, nothing is really full proof at preventing caching on the client side.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement