Problem with mobile redirection login I've implemented this mobile redirection logic but for the past 10 days I've been getting weird behavior(with www version, mobile data is shown sometimes).

Here is Mobile Theme return logic

Also I find different behavior if I uncomment "return 'bartik' in the last line below.

  function mobile_detect_custom_theme() {

        if (strcasecmp(substr($_SERVER['HTTP_HOST'], 0, 2), "m.") == 0)
        {
            return 'mobile_theme';
        }

//return 'bartik'; //IF I uncomment it I find different behavior
    }


In Hook_book I've setup this redirection logic:

//wantdesktop is set if user has specifically selected Desktop version
if (@$_COOKIE['wantdesktop'] == 'true') {
        if (strcasecmp(substr($_SERVER['HTTP_HOST'], 0, 2), "m.") == 0) {

            $url = "http://www.example.com/" . $_SERVER['REQUEST_URI'];

            header('Location: ' . $url, TRUE, 302);
            exit(0);
        }

        return;
    }



//wantmobile is set if user has specifically selected mobile version
      if (@$_COOKIE['wantmobile'] == 'true') {
        if (strcasecmp(substr($_SERVER['HTTP_HOST'], 0, 2), "m.") != 0) {

            $url = "http://m.example.com/" . $_SERVER['REQUEST_URI'];



            header('Location: ' . $url, TRUE, 302);

            exit(0);
        }

        return;
    }



//BROWSER DETECTION LOGIC HERE which fills $browser['ismobiledevice'] with true/false


    if ( $browser['ismobiledevice'] == 'true') {
        //check the current domain
        if (strcasecmp(substr($_SERVER['HTTP_HOST'], 0, 2), "m.") != 0) { //not a mobile url
            $url = "http://m.example.com/" . $_SERVER['REQUEST_URI'];
            header('Location: ' . $url, TRUE, 302);
            exit(0);

        }
    } else { //desktop
        if (strcasecmp(substr($_SERVER['HTTP_HOST'], 0, 2), "m.") == 0) { //is a mobile url

            $url = "http://www.example.com/" . $_SERVER['REQUEST_URI'];
            header('Location: ' . $url, TRUE, 302);
            exit(0);

        }
    }


What is wrong with this logic?
May I suggest an alternative approach of using the Themekey module. It was built to solve the exact issue you are facing:


  ThemeKey allows you to define simple or sophisticated theme-switching rules which allow automatic selection of a theme depending on current path, taxonomy terms, language, node-type, and many, many other properties. It can also be easily extended to support additional properties exposed by other modules. In combination with Drupal's theme inheritance you can easily achieve features like:
  
  
  individually-styled channels
  a front-page / "splash" screen
  a date/time-selected Christmas theme
  mobile themes for different auto-detected mobile devices
  special themes for "limited" or "old" browsers
  content, user, or role -specific themes
  indicating your environment (production, staging, testing, sandbox, … )
  testing your redesign safely on a live server
  


The important thing to note about Themekey, is that the module works well with the external caching as well. Do check out its documentation for more.