Prevent Caching

Ask about a PHP problem here.
Post Reply
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Prevent Caching

Post 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.
User avatar
element
Posts: 8
Joined: Mon May 16, 2011 7:20 pm
Location: Germany

Re: Prevent Caching

Post 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]
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Prevent Caching

Post 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?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Prevent Caching

Post 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.
Image
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Prevent Caching

Post 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.
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Prevent Caching

Post by unemployment »

Caching fixed... looks like I have to close my browser and reopen it to fix the issue. Seems fine now.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Prevent Caching

Post 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
Image
Post Reply