Page 1 of 1

Prevent Caching

Posted: Fri May 20, 2011 12:41 pm
by unemployment
How can you prevent your browser from caching a page? I think that my news feed is being cached and is causing new posts to not be displayed even after a refresh. Eventually, the posts will show up after about 20 refreshes. I know my query is right so I didn't know if there was a way to stop page caching.

Re: Prevent Caching

Posted: Fri May 20, 2011 1:13 pm
by element
[syntax=php]
header("Expires: -1");
header("Cache-Control: post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
[/syntax]

usually you can also set the html meta tags

[syntax=xhtml]
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<meta http-equiv="expires" content="0">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
[/syntax]

Re: Prevent Caching

Posted: Fri May 20, 2011 1:51 pm
by unemployment
I'm starting to think I just have some weird bug in my code because sometimes on refresh news feed items get removed then i'll refresh again and some will get added back in then i'll refresh again and they'll all be there. Every heard of this happening?

Re: Prevent Caching

Posted: Fri May 20, 2011 3:12 pm
by jacek
You can also make the URL random if its an ajax request.

Are you loading any of this news via ajax, I have found that javascript seems to ignore the cache control sometimes.

So make a request for news.php?random=time() or something and it would fix it.

Re: Prevent Caching

Posted: Fri May 20, 2011 4:10 pm
by unemployment
jacek wrote:You can also make the URL random if its an ajax request.

Are you loading any of this news via ajax, I have found that javascript seems to ignore the cache control sometimes.

So make a request for news.php?random=time() or something and it would fix it.


I would love to load this via an ajax request but haven't gotten to that yet (mainly because I have no idea how to do it).

I just finished the feed today... well pretty much at least. The privacy settings are in and that array merge worked perfectly for the situation.

Next for the feed will be creating the ajax pagination / more button to load then next 20 feed items and in an ideal would, just load the whole feed via ajax. Plus, it would be nice to have ajax updates.

So many things i'd love to do..... yet soo many things i'm not capable of yet :)

But to answer your question again, I am not using ajax to load the feed.

Re: Prevent Caching

Posted: Fri May 20, 2011 4:13 pm
by unemployment
Caching fixed... looks like I have to close my browser and reopen it to fix the issue. Seems fine now.

Re: Prevent Caching

Posted: Fri May 20, 2011 4:17 pm
by jacek
unemployment wrote:Caching fixed... looks like I have to close my browser and reopen it to fix the issue. Seems fine now.

That's odd, but good news :D