-1

I created a PHP website from scratch and everytime I upload the changes on my host account, I have to clear the chache/cockies from my browser settings. How can I do it by code? I do not want the user do this by itself in case I make another changes. Thank you!

John Locke
  • 185
  • 1
  • 17

1 Answers1

0

You would need to send the proper headers to prevent caching. For pure PHP generated stuff, something like this sent once per page before ANY other output is sent will work -

<?php
  header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Pragma: no-cache");
?>

For non-programmatically generated files where you can't do this (images, .js files, .css, etc) it is possible to configure the web server to tell the client to limit cache lifetime. Of course, it is up to the client to follow instructions...

From https://www.liquidweb.com/kb/how-to-configure-apache-2-to-control-browser-caching/

<IfModule mod_expires.c>
# Turn on the module.
ExpiresActive on
# Set the default expiry times.
ExpiresDefault "access plus 2 days"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType text/css "now plus 1 month"
ExpiresByType image/ico "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType text/html "access plus 600 seconds"
</IfModule>

Similar configuration is possible for nginx and other http servers - see https://www.digitalocean.com/community/tutorials/how-to-implement-browser-caching-with-nginx-s-header-module-on-centos-7

ivanivan
  • 2,155
  • 2
  • 10
  • 11