Offering each user a custom login destination

A post on the drupal.org discussion forum recently caught my attention:

I’m running a CMS which restricts access by using OG. As such users are registered to one group or another and in order to do away with unnecessary clicks I’d like a user to be automatically directed to the group front page to which they belong. As such if I belong to the ‘design’ group, the system should automatically redirect me to the ‘design’ home page when I log in. Is there a module that will enable this for me?

I use Drupal’s core profile module together with the Login Destination module to offer a “login page” feature on my sites. Here’s how to do it on yours.

1. Set up a Profile field

First enable the profile module, set its permissions accordingly and create a new profile field:

  • Category: Login settings
  • Title: Login page
  • Form name: profile_login_url
  • Type: URL
  • Explanation: Enter the full URL for the page you would like your login destination to be. For example, enter http://example.com/group/design if you want to go to the Design Group home page immediately after logging in.
    • It’s up to you, but I usually set this profile field to “Private” instead of “Hidden” so site members can click on it when viewing their own profile.

      2. Set up a Login Destination

      Finally, configure Login Destination to use a PHP snippet and paste this in:

      global $user;
        if (!$user->profile_login_url == "") {
          // check to see if Login URL is set
          return $user->{profile_login_url};
        } else {
          // go to the referring page (yes, referrer is mispelled in the HTTP specification)
          $referrer = strtolower($_SERVER["HTTP_REFERER"]);
          return $referrer;
      }

      If the user hasn’t set a Login URL, the referring page is used instead. This is much better than sending a user to his or her profile. Imagine visiting a private page without being logged in and getting an “access denied” message. After logging in, you are then sent to the user profile instead of the original page. I find this incredibly annoying so I opt to use the referring page instead.

      3. Check your account settings

      Now you and your users will have a new “Login settings” tab on the account settings page with a “Login page” setting.

      Login settings