Take the 2-minute tour ×

Essentially what I am trying to figure out is on sites such as say Facebook, the side bars such as "People you may know" are dynamically generated on load. If you navigate to another page and then using your browser, click 'back' in history, the side panels with dynamic content are the same as they were when you first viewed them.

On my site, when you go back in your browser history my dynamic content isn't the same as when you first say it, but PHP runs and puts new dynamic content on the page.

How do I skip running PHP/SQL Query if the user is clicking 'back' or 'forward' through his history?

Here is the AJAX call I am making:

<script type="text/javascript">
$(document).ready(function() {
        type: 'post',
        url: '/ajax/ajax_common.php',
        async: true,
        cache: true,
        data: { [data here] },
        success: function(data) {

It gets the data, but when I go to another page and click back it changes and has not saved to cache. The AJAX query calls a function that generates some random photos.

share|improve this question
Keep in mind that facebook uses AJAX for 99% of its content. That said, the "Caching" you're seeing is just pre-fetched information that gets re-loaded. Chances are your back button is making a brand new request, not going back in the AJAX request queue. –  Brad Christie Nov 19 '11 at 22:54
You need to set proper Expires headers for your scripts so the browser will cache the results. –  Michael Mior Nov 19 '11 at 22:56
Would this then be heaving usage of memcache? Facebook was an example, there are many other sites that do this. –  Fase Nov 19 '11 at 22:56

1 Answer 1

You can set session_cache_limiter() to private. Alternatively, you can also achieve the same thing, but with more control by setting the headers using header() manually.

As you would like to cache data for certain divs, the best way to do this would be to use javascript. You can use JQuery to use AJAX to fetch data into those divs.

For example, the data might be contained in memberlist.html and memberranking.html.

Then using jQuery.ajax(), you can fetch memberlist.html and memberranking.html, and then insert that into your page. At the same time, when you run the AJAX request, set cache to true and the result will be cached by the browser.

Obviously this won't work for users without javascript enabled, so you might have to look into SSI (server side includes).

Alternatively, you could use object level caching using a cache such as APC or memcached. This will need to sit in your PHP layer, and you will need to be able to process a request to see whether a cached copy could be used. You will also need to write your own set of procedures to remove objects from the cache if an update or deletion in the underlying data occurs.

share|improve this answer
Expanding on this, how do I use this to get new content if it's a fresh page load, and use the old content if it's loading from history. –  Fase Nov 19 '11 at 23:12
I had to unmark this as the answer as I noticed some problems from it. 1) If you login on the page, it shows you as not logged in until you load another page. 2) If you close and open the browser it shows you as logged in when you are not. I need it to isolate the cache to certain DIVs. –  Fase Nov 23 '11 at 2:26
I have edited my answer. –  F21 Nov 23 '11 at 3:07
The only method above I can use is the ajax call through jQuery with cache: true. This however is not caching my results. –  Fase Nov 27 '11 at 21:38

Your Answer


