php and mysql - avoiding cache - php

I have a problem that one of my users, make an insert into my mysql database, but when he goes to another page to list these inserts, it only return old data that was previous there. He can only see the changes after several minutes or even hours.
I read that If I add a random number in my url, I can avoid cache, but still not working.
I tried to put these headers on php.
header("Cache-Control: private, no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
And I tried this one too:
SELECT SQL_NO_CACHE...
None work. Still don't get any visible changes.

Try using the Expires headers in your htaccess file. You will need to ask your user to Ctrl+F5 (Force refresh) for it to correctly take effect;
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType text/html "access plus 0 seconds"
</IfModule>
This will tell the browser not to cache the text and html within that directory and any subdirectories (unless otherwise specified). I had a similiar issue and resolved it with this when I realised my caching was affecting the update of live data.

Related

Constantly forcing reload of a page on Wordpress

I have a Wordpress site running a plugin called OpenHours. Basically, we program in our operating hours to the backend of the site and then have a page /hours/ which shows them. I have disabled every possible Caching feature in Wordpress and tried editing the .htaccess file with the below:
# DISABLE CACHING
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
<FilesMatch "\.(css|flv|gif|htm|html|ico|jpe|jpeg|jpg|js|mp3|mp4|png|pdf|swf|txt)$">
<IfModule mod_expires.c>
ExpiresActive Off
</IfModule>
<IfModule mod_headers.c>
FileETag None
Header unset ETag
Header unset Pragma
Header unset Cache-Control
Header unset Last-Modified
Header set Pragma "no-cache"
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
</IfModule>
</FilesMatch>
We have a mobile app that basically links users to the webpage through their phone's browser, but anytime the link is clicked the page doesn't update (The plugin is configured to show "Currently Open" or "Currently Closed" based on time of day). Is there a way to force the page to update and not cache whenever it is visited?
I tried a query string but unfortunately the spot we insert the link in the app does not support any type of coding, just a straight link, so /hours/?rnd="+Math.random() and /hours/?rnd="+new Date().getTime() haven't worked.
You can set new headers for a specific page using the default PHP header function.
add_action("init", function() {
// TODO: Replace with the actual page ID or title
if(!is_page("page-without-cache")) return;
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.
}, 10);
We used the init function to make sure that the header is sent before rendering the page.
And if you are using any cache plugin, you should be able to create a list of URLs or directories that you do not want to include or that you do not want to be cached. For example, in the WP Super Cache plugin, the option is located in the Advanced tab > Accepted Filenames & Rejected URIs section. By default, these paths are excluded from cache.
wp-.*\.php
index\.php
And in addition, WP have dedicated functions called nocache_headers or wp_nocache_headers, which you can also use to prevent caching. The functions are also using the default PHP header function.

I don't get the website's up-to-date html

I changed the .htaccess file to cache everything for a year.
After a while, I noticed that I don't see the updates I make to the website.
I figured out, that because I changed the default cache to a year, somehow the html file got cached for 1 year, and even though I've changed it back, still doesn't work.
The only way I could make it update is to use different links (like www.domain.com/newlink insead of www.domain.com/oldlink), but that doesn't repair my frontpage, as it is www.domain.com, and I can't change that.
Do you have any solution for my problem?
P.S. It isn't my browsers cache, as I have cleared it's cache, I even reinstalled it, and if that's not enough, I can't see the updated version on my phone.
P.P.S. I can see that the http request doesn't even reach the server for the www.domain.com link.
P.P.P.S. I tried using a proxy, and the page worked as it should, so I think it's only cached at my ISP.
Can you try to explicit say not to cache to apache in the htaccess. Also restarting your apache server might work.
<filesMatch "\.(html|htm|js|css)$">
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>

All browsers keep caching my codes - Driving me nuts

Now this bug has lasted long enought. I have tried everything to stop the caching. Everything on the following stackoverflow links:
Chrome - Disable cache for localhost only?
Disabling Chrome cache for website development
Using <meta> tags to turn off caching in all browsers?
How to prevent Browser cache for php site
How to control web page caching, across all browsers?
How to prevent http file caching in Apache httpd (MAMP)
And also i tried to rightclick on the reload button on chrome and choose reload page complete and empty cache and reload page completely. I use PHPStorm as IDE but i also tried switching to notepad++ in case PHPStorm wasn't saving the file properly. Tried setting timestamps in the header so my requests to the page isn't identical. I am running on localhost XAMPP. Any solutions/ideas/guesses are more than welcome.
Note
Restarting XAMPP actually clears the cache (or at least forces the browser to reload the content). I have no idea why. Thanks in advance
Note
My PHP codes are being cached as well. When i edit my PHP codes and i refresh the page, it runs the old codes again. It's probably because the browser still shows the cached version of the page but thought it was worth mentioning.
There isn't much info here; but you might want to have a look at your php.ini and especially your revalidate_freq. It should preferably be 0 for development; but definitely not bigger than then default of 2 seconds.
Create a .htaccess file under root directory and put the below code inside .htaccess file.
check if you have mod_rewrite enable in the server (optional).
Add it stops caching the pages
#Initialize mod_rewrite
RewriteEngine On
<FilesMatch "\.(html|htm|js|css)$">
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, 12 Jan 1980 05:00:00 GMT"
</IfModule>
</FilesMatch>
Please check below mentioned solution, It will help you.
Step 1:
In HTML head tag you should add below mentioned lines.
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
So with this when user visits your page, every time new content will fetch from server.
Step 2:
By using .htaccess you can prevent page 100% being cached by browser.
<filesMatch "\.(html|htm|js|css)$">
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>
Step 3:
If your page is in PHP than add this line on the top of the page.
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");
header("Expires: 0");
Let me know if it not works.
i install a chrome Extension that clear cache. i config it to clear cache with CTAL+Q - so insead of refresh page with F5 or CTRL+R i use this Extension - it solve my developin cache problems ( i use polymer framework - and i have same problems as you )
i use this extension

How to troubleshoot "Pragma: no-cache" with Concrete5 on Debain?

Setup
Debian GNU/Linux 7.6 (wheezy) will full access to Apache and virtual
host files.
Concrete5 CMS: http://www.concrete5.org/
Problem
Our website is sending Pragma: no-cache in headers which is stopping a number of optimisations from working - including the CloudFlare service: https://www.cloudflare.com/
Solutions which didn't work
When researching, we either didn't understand the answers or they seemed to be for specific use cases (like Oracle or php frameworks we're not using) but we did try the following:
1. Force caching via the site's .htaccess:
<FilesMatch "\.(ico|jpeg|png|gif|js|css)$">
Header unset Cache-Control
Header unset Pragma
</FilesMatch>
2. Force caching via Concrete5's header.php file
header("Cache-Control: max-age=2592000"); //30days (60sec * 60min * 24hours * 30days)
3. Search site's root for no-cache using grep
$ grep -r "no-cache" * .
backup/databasebackup.php:header('Cache-Control: no-cache, must-revalidate');
concrete/core/controllers/single_pages/login.php: header("Cache-Control: no-store, no-cache, must-revalidate");
concrete/core/controllers/single_pages/login.php: header("Pragma: no-cache");
concrete/js/tiny_mce/plugins/spellchecker/rpc.php:header("Cache-Control: no-store, no-cache, must-revalidate");
concrete/js/tiny_mce/plugins/spellchecker/rpc.php: header("Pragma: no-cache");
concrete/libraries/3rdparty/securimage/securimage.php: header('Cache-Control: no-store, no-cache, must-revalidate');
concrete/libraries/3rdparty/securimage/securimage.php: header("Cache-Control: no-store, no-cache, must-revalidate");
concrete/libraries/3rdparty/securimage/securimage.php: header("Pragma: no-cache");
But after looking at the files/scripts Concrete5 is setting no-cache on (login, database backups, text editor configs etc), we kind of understand why - plus these seem to be for specific files, not the entire site right?
4. Make a blank php file, request it and check the header
The blank file was served with caching on so we suspect that php is the culprit - but have no idea how to isolate the cause sorry.
Question
How do we troubleshoot and fix this issue?
Skill level
We do front-end design and understand the basics on how to setup and serve a CMS but don't have much experience with server configuration or troubleshooting cache issues.
We have command line access to the server and pretty much have full access to Debian, Apache, and the site's install.
Any help would be much appreciated.
Cheers
Ben
Update
To add max-age in a PHP script:
header("Cache-Control: max-age=xxxx");
Where xxxx is the number of seconds to cache, zero for no cache.
OR
If you configure by Content-Type (MIME Type)
header('Content-Type: text/html; charset=utf-8');
Header Set Cache-Control "max-age=0, no-store"
to configure cache by Content-Type (MIME Type):
In .htaccess ot httpd.conf
ExpiresByType text/html "access plus 30 day"
ExpiresByType text/css "access plus 30 day"
ExpiresByType text/javascript "access plus 30 day"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
If these methods are not working you need to majke sure the modules are loaded.
You need access to httpd.conf
LoadModule expires_module libexec/mod_expires.so
LoadModule headers_module libexec/mod_headers.so
AddModule mod_expires.c
AddModule mod_headers.c
...
AddModule mod_gzip.c
Note that the load order is important in Apache/1.3x, mod_gzip must load last, after all other modules.
For Apache/2.0:
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
end of Update
You should add the same cache based on MIME Type as well as (or rather than) file extension.
The cache should be max-age.
The W3C says max-age takes precedence over all other cache headers.
To troubleshoot you are doing well already if you are not getting "Internal Server Error 500"
In FireFox or Chrome
Right Click page
Select Inspect Element
Go to the "Network Tab"
Change Type from "All" to "HTML"
Click on the HTML page in the list
You should be able to see exactly what is in the HTTP Response Header.
FireFox
Chrome
Finally got to the bottom of this, some of the Concrete5 Blocks on pages are stopping the pages from being cached.
If we turn on "Force full page caching" then we see some weird behaviour because the functionality is frozen by the cache, so we've had to turn it off.
Essentially, we can't cache the site fully because of the functionality in blocks on the page. We can only use APC caching.

not cache image linked in stylesheet

when I am debugging to ensure that some files are not cached and therefore reloaded anew each time, I frequently link files with something like
<script src="script/js.js?<?=time()?>"></script>
Is there some way I can do something in css to ensure background images are reloaded every time the page loads without making the style something other than a .css file? (php with css header for example)
#bg_div{
background: url(../images/darrow.png.....)
}
Thanks!
You could use .htaccess like this:
<filesMatch "\.(gif|jpg|png)$">
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 "Mon, 26 Jul 1990 05:00:00 GMT"
</ifModule>
</filesMatch>
This prevents all images to be cached. If you put the .htaccess file in a specific folder then images in this folder doesn't get cached.
Note that on sites with heavy load cache control should be carefully planned. Using a htaccess directive like this can VASTLY affect page load times.

Categories