Ahhh, now thats refreshing!
August 9, 2007 11:11 AM   Subscribe

How do I make sure a page loads with a fresh copy and not from the users cache?

I am working on a small site. It is basically just a single message board. The page has a form where people can enter some text and their name and hit submit. Below the form is the message list.

The form sends its data to a PHP script which cleans the text a bit and then adds it to a MySQL DB. It then uses a "Location: 'main.php'" command to go back to the main page. The main page with the form is just a short PHP script that queries the DB and displays all the results.

The problem is, on some computers (my work computer for instance) after adding a message using the form, when it sends them to the main page it shows a cached copy of the main page, and so their message isnt visible. Hitting "refresh" will reload the page and their message is then visible.

How do I go about forcing the page to "refresh" every time it is vistited, or at the very least, after the form has been submitted? Any help you guys can provide would be most appreciated!
posted by LoopyG to Computers & Internet (7 answers total) 2 users marked this as a favorite
The Cache-Control, Pragma, and Expires HTTP response headers are your friends here. They overlap in odd ways, and I don't remember what the current state of standards compliance is in the browser population, but I bet you can google that info up.
posted by hattifattener at 11:18 AM on August 9, 2007

Best answer: hattifettener beat me to it. Adding
<meta http-equiv="pragma" content="no-cache" />
to the head section of your documents should work, but I'm not sure about browser support.
posted by adamrice at 11:23 AM on August 9, 2007

Response by poster: Hi adamrice, I'll try it tonight (cant be doing such things from work =). Anyone else though, have any more knowledge abotu that meta tag? Does it work in most browsers, is there a better way?
posted by LoopyG at 11:26 AM on August 9, 2007

It's almost always better to use a real HTTP header than to use a <META> tag to imitate one. (What the meta http-equiv tag does is say, "Pretend I supplied the following header...").

The Pragma: header is the old way of doing things; the Cache-Control: header is the new (post-1999, but MSIE may not have caught up yet) way. Expires: has a different purpose but may be useful for what you're doing anyway. Look 'em up.
posted by hattifattener at 11:43 AM on August 9, 2007

Agreed that real headers are better. http-equiv meta tags are easier to work with. At the very least, something quick and dirty to get you going.
posted by adamrice at 12:06 PM on August 9, 2007

Note that browsers are free to ignore this or implement it in strange and backward ways *glares at Microsoft* The best way to do this is to rewrite the link to a 'dynamic' page that will (hopefully) never be cached. Something like "Location: 'main.php?no-cache=bigrandomstringofeverchangingnumbershere'" will force even old, braindead browsers to force-fetch the content.
posted by Skorgu at 1:30 PM on August 9, 2007

I second Skorgu's suggestion. I used HTTP headers on a project sometime back, but some browsers behaved badly and didn't want to listen. Not sure if it was my scripting mistake, or if it was the browser.

But the one solution that really solved my caching problem is dynamic URLs. It's as simple as attaching an additional random number at the end of it. So everytime you use PHP to call up a page, just do something like:

Header("Location: main.php?" . date("YmdHis"))
posted by arrowhead at 8:19 PM on August 9, 2007

« Older how many americans have died under individual U.S....   |   Alas poor Ipod Newer »
This thread is closed to new comments.