tags:

views:

62

answers:

2

Hi guys,

I have a search box on my site, whereby the user enters a term and results are displayed. When they click one of the results (a product) they get taken to the product page. When they then hit the back button, they get shown a "Warning: this page has expired" message.

I am using CakePHP on Apache, and have been advised that I need to change session.cache_control? I have tried chaning it to private in htaccess, but it does not seem to have helped.

Any help much appreciated.

Cheers, D

+2  A: 

This is nothing to do with CakePHP nor sessions. Indeed, the issue arises in all web programming languages.

The solution is to make the search results page cacheable - I would imagine that you're not updating your catalog every 5 minutes?

To make it cacheable you need to ensure that the search terms are sent using a GET rather than a POST, then set the right headers to enable the browser to cache the page, e.g.

 header('Cache-Control: max-age=360'); // allows browser to keep for 1 hour

....and if you're using sessions which may constrict the visiblity of certain products in the search:

 header('Varies: Cookie');

C.

symcbean
Second his mention of using GET parameters. Remember the guideline: use GET for retrieving data (idempotent), use POST to alter data. I break it all the time, but it's a good rule to try to follow.
Travis Leleu
Thanks guys, yes, I have changed it to use GET, and all working good now.Cheers
Dashman
A: 

You can also use the Post/Redirect/Get design pattern pattern to avoid this. For example:

function index($searchTerms = null) {
    if (isset($this->data['Model']['search_terms'])) {
        $this->redirect(array($this->data['Model']['search_terms']));
    }
    // your normal code here.
}

This will result in URLs like /controller/action/search+terms instead of /controller/action?search_terms=search+terms, and search terms will be passed to the action as a parameter (ie. $searchTerms in this case).

deizel