6

I wrote a callback in my hook_menu() implementation, wich won't work the way it is intended to because of caching. The callback just returns some text depending on a cookie. Something like that:

function _show_logoutpage(){
    if($_COOKIE['foo'] == true) {
        return 'foo';
    }
    else {
        return  'bar';
    }
}

The code is called after the user is logged out so I would say the user is anonymous. Problem is the the output is cached (since the last menu cache clear) so the logic won't work. Any idea how to fix this in a simple way?

3

Change return to echo and exit; or disable the page cache.

function _show_logoutpage() {
    if ($_COOKIE['foo'] == TRUE) {
        echo 'foo';
        exit;
    }
    else {
        echo 'bar';
        exit;
    }
}

OR

function _show_logoutpage() {
    global $conf;
    $conf['cache'] = CACHE_DISABLED;
    if ($_COOKIE['foo'] == TRUE) {
        return 'foo';
    }
    else {
        return  'bar';
    }
}
5

$GLOBALS['conf']['cache'] = FALSE;

See also module CacheExclude, here are some more details about it:

This module provides a simple way to exclude certain pages from being cached. Sometimes you want all pages to be cached for anonymous users except for one or two pages that have dynamic or random or rotating content. If those pages are cached, the dynamic parts cease to be dynamic. This module allows an administrator to selectively exclude certain paths from being cached so that dynamic content is actually dynamic.

1

I don't believe the problem is that menu items are cached. Could it be that the page result of that menu path is cached for anonymous users (thus disregarding the $cookie status for all users except first one to visit the page)?

What version of Drupal are you using? What are you page caching settings?

-2

That could be done using JavaScript (client side on page load), instead of PHP (server side on cache build).

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.