2

My drupal site uses normal cache system, as you'll probably know, the cache_page table is updated using as primary key the REQUEST_URI. This is great because when an anonymous user requests a page, drupal serves it to the client from the cache_page, and the best part is that all variables in the GET request are part of the cache id for serving that page.

So for example if I have these URL's

  • http://domain.com/gallery
  • http://domain.com/gallery?list_mode=thumbs

Both url's are different in the cache_page table.

The problem comes here, I want anonymous users to remember the last option that selected as "list_mode", so the next time they come they'll get one or another list mode by default. So I added list_mode as a SESSION var. The problem is that accessing

http://domain.com/gallery

as anonymous, the page is served from cache with the default list_mode. (whose value is not "thumbs")

I don't want to stop using cache_page for this url (this was a possible solution), and the ideal thing would be adding some way this SESSION variable to the REQUEST_URI so the page will be served as it should , in this case would consist of translating:

http://domain.com/gallery TO http://domain.com/gallery?list_mode=thumbs

before checking the cache, so the user gets the second URL served instead of the first.

hook_boot would be a good place to do that, but I don't think that hacking the request_uri is a good idea, so, is there any other way to do this approach ?

Thanks for your help.

PS: I found a similar issue, the user makes use of the RewriteCond to achieve what he wants, and asks for a better solution, but the only answer advises to disable caching for that pages: Cache page based on cookie value and URL

1

On the other answer, the page cache is bypassed on the first hit. Subsequent hits do hit the page cache because the cookie will be set then.

To answer your question, have some client side javascript that sets a cookie called gallery_list_mode. If no selection is made, set it to 0 or delete it. If user selected thumbs, have the javascript set the gallery_list_mode cookie to thumbs. Then inside of your settings.php drop this in

<?php
// Check if cookie is set and user is on a gallery page.
if (!empty($_COOKIE['gallery_list_mode']) && $_SERVER['REQUEST_URI'] == '/gallery') {
  $ok_list = array(
    'thumbs',
  );
  if (in_array($_COOKIE['gallery_list_mode', $ok_list)) {
    // Add cookie value to page cache id.
    $_SERVER['REQUEST_URI'] .= '?list_mode=' . $_COOKIE['gallery_list_mode'];

    // This line might be needed.
    //$_GET['q'] .= '?list_mode=' . $_COOKIE['gallery_list_mode'];
  }
}
?>

This will make http://domain.com/gallery point to the cache entry for http://domain.com/gallery?list_mode=thumbs if the gallery_list_mode cookie is set to thumbs.

As a security feature you should make sure to only accept values for gallery_list_mode that you want. That is what the $ok_list array is for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.