2

I am currently in the process of developing a custom module that will redirect a user on entry to an interstitial landing page. This page will closely replicate how forbes.com functions. The landing page itself is a page template within the custom module that displays a view, an ad, and links back to the originally requested page. All of the pieces are in place and function as intended except for the hook when caching is enabled. After researching hook_boot, hook_init, and hook_page_alter, I have found that none of these are working when caching is turned on or in the case of hook_boot because drupal functions were not loaded when the hook was called. I feel hook_boot is the closest solution at this point but after reading the comments in the api it was said that you shouldn't invoke any drupal modules inside of that hook. Is there a way I can detect if the user is anon without user_is_anonymous()? Should I write this hook without any drupal functions? Should I bootstrap drupal in hook_boot similar to how its done in the core module file statistics/staticstics.php?

Here is my code for hook_boot:

function my_module_boot() {
  // If anon proceed
  if(user_is_anonymous()){
    if(isset($_COOKIE['welcome_page'])){
      // do nothing
    } else {
      $cookie = filter_xss($_COOKIE['welcome_page']);
      // If user saw welcome page and has cookie- skip redirect
      if($cookie != 1) {
        setcookie('welcome_page', 1, time()+86400);
        $destination = drupal_get_destination();
        drupal_goto('welcome', array('query'=>$destination), $http_response_header = 301);
      }
    }
  }
}

My goal is to keep all of the code contained inside of the module. I'd rather not use any other contrib module at this point because my module is close to completion. This redirect should only get triggered if the user does not have the cookie and is anon. Finally I do not want the site to appear before the landing page.

Thanks for any help.

Solution:

function my_module_boot() {

  if(isset($_COOKIE['welcome_page']) || isset($_COOKIE['welcome_page_admin'])) {
    // do nothing
  } else {
    $cookie = filter_var($_COOKIE['welcome_page'], FILTER_SANITIZE_STRING);
    // If user saw welcome page and has cookie- skip redirect
    if($cookie != 1) {
      $destination = '/';
      if($_GET['q']) {
        // Microtime for IE9 to properly redirect
        $destination = $_GET['q'] . '?a=' . microtime(true);
      }
      // Redirect and set cookie
      setcookie('welcome_page', 1, time()+86400);
      header("HTTP/1.1 301 Moved Permanently");
      header("Location: /welcome?destination=" . $destination);
      exit();
    }
  }

}

function my_module_user_login(&$edit, $account) {

  // Expires in one year for authenticated
  setcookie('welcome_page_admin', 1, time()+31556926);

}
0

Your suspicions are correct. If you want to do this server side on a cached page, you'll need to use hook_boot() and you won't be able call most Drupal functions because Drupal hasn't been bootstrapped at the stage that hook_boot() runs.

It seems to me that this is much better suited to client side. I would move this to a small js file (put it in the head if you're worried about the the redirect happening too late).

  • Thanks for the response Adam. Your suggestion sounds valid. I ended up pushing forward with hook_boot, and reworked my code to avoid the use of any Drupal functions. I had to add hook_user_login to set a cookie since the user_is_anonymous() function was not available. This will prevent the interstitial for auth users including admins. – john4008 Apr 6 '15 at 14:31

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.