I crete a web service in php and which returns JSON. It works fine but it seems to be cache results I changed values in the table and the service doesn't reflect this change. I included the following code but it doesn't seem to work. in my browser I am still getting the old values. How can this be rectified?
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
You may want to check your Ajax library (client) documentation see if you can disable its caching mechanism.
If you wish to disable cache on server side, refer to "How to control web page caching, across all browsers?"
Related
I'm still inexperienced as a web developer so I have exhausted my knowledge on my current problem.
I recently made changes to files on my one website and uploaded them via FTP to the server. When I go to the website it's still executing the old code.
I cleared all my cache and tried on different devices. Even after changing the file names for example "logout.php" to "logout_code.php" as well as link references to those names when I click on the logout button it still goes to "logout.php".
Can anybody shed some light?
Try setting a no-cache header on the page that redirects to the logout page.
header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
The above snippet goes at the top of the page before any other output is sent. (within a <?php ?> tag of course)
I have a systen in place where I have two-layered cache on the website. Every request checks for cache on the web server and if it finds the generated page that has been cached, it returns the cache instead of generating it again.
For example:
User A visits website URL http://www.example.com/home/
Server does not find cache
Server generates the page
Server writes the page to cache
Server returns the generated page to User A
User A is happy
and
User B visits website URL http://www.example.com/home/
Server finds cache
Server returns the cache instead of generating the page again
User B is happy
All that works without problems. But I also want to add an option that browser would not ping the server again (saving servers time of checking if cache exists or not) and use its own cache instead.
User A visits URL http://www.example.com/home/ again
Browser has that page in cache
Browser loads the page for the user from cache
I cannot get the latter working. During original page generation, I am sending to the user with the page the following headers:
header('Cache-Control: public, max-age=10000, must-revalidate');
header('Expires: Fri, 03 Feb 2012 01:59:45 GMT');
But when I check for it with Firebug or Chrome Developer Tools it does not say it is using a cache, instead asking for the data from the server again. I know I must be doing something wrong, since I have the same thing set up for static files like Javascript, and that works.
To test this I didn't just try reloading the page, I created links on the website and moving between those links it asked for the pages from server each time.
Am I missing something?
EDIT:
Alright, apparently what happened was that server sent "Pragma: no-cache" automatically every time. Does anyone know why server would do that? This kept the browser from using cache.
If session is enabled for that page/url, the Pragma: no-cache-header will be added to the http header which prevents the browser from using the cache.
if you use session_start
PHP will add
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
the whole point of PHP is to provide dynamic pages after all.
to stop this ...
session_cache_limiter('');
session_start();
and you can then write your own headers based on the content you provide
When using Codeigniter or a simple php page, loadin my test page once I get the normal 200 status but if it is cached I get the 304 message. I realize this is just a signal that the resource is cached but could I avoid it?
If so, how?
Thank you.
... or you could just do
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
in your PHP files right at the top. That way you have control over which pages not to cache and leave Apache running as it should.
If you don't want the page to be cached by browsers you need to configure the apache server to send the appropriate headers.
I've moved a older site to a new server, and the client has found a very odd behaviour.
Very close to the end, I have this code:
if (!$this->cache) {
header('Expires: '.gmdate('d M Y H:i:s', 946684800).' GMT');
header('Cache-Control: no-cache');
header('Pragma: no-cache');
}
Now the odd thing is the Cache-Control line doesnt work.
After packet sniffing I see this:
Expires: 01 Jan 2000 00:00:00 GMT
Cache-Control: max-age=300, public
Pragma: no-cache
The order of the headers is exactly how I set them, but the Cache-Control is completely different. I've grepped my code for any mention of cache-control and there is only that mention, and another one designed to force caching in a different file but it is a different line to what I'm seeing so it cant be the culprit.
Does anyone know why Cache-Control is changing?
If you are using sessions, it's possible that PHP is overwriting them. Take a look at session_cache_limiter() in the manual.
Alternatively, you can try setting those headers after you call session_start().
(Edit: I missed the bit about "Very close to the end," so maybe this isn't your problem.)
I think you are running Squid, you should check its config for:
header_replace Cache-Control max-age=300, public
Hey, I'm trying to change the name of a file being downloaded through my site off an external server.
Currently whenever I modify the header information, it just takes a reallllllyyyyyy long time for the download to start, so is there any way to do it without changing the headers? Or if not any reason why it would be taking forever to load with my headers?
This is what I'm doing
#header("Cache-Control: no-cache, must-revalidate");
#header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
#header("Content-Length: ".getfilesize($url));
#header("Content-Disposition: attachment; filename=".$name);
#header("Content-type: audio/mpeg;\r\n");
die(#readfile($url));
Could it be done in javascript or something instead? And could i make it so all downloads start without opening in like windows media player, quicktime etc.
Thanks :)
It takes a "reallllllyyyyyy long time" because your loading the file to your server first and then send it to the client everytime someone is clicking the download link.