Login Destination project page.

What is the Login destination module?

The login destination module provides a way to customize the destination that the user is redirected to after login

What features does the Login Destination module offer?

The Login destination module provides very nice features including:

  • A customizable login destination page in the form of a static URL
  • Ability to customize the login destination using php code
  • Only apply the login destination rules when logging in from certain pages
  • Setup php rules of evaluating when to apply the login destination rules

You can get an inside look at the module and view a demo of its features on the Login Destination Module Preview

Drupal 6.x

You can provide a PHP snippet that returns a string or returns an array: (ONLY when there is a GET query at the end of the url - like /example?var1=value)
Example: (note the absence of the "?" sign)

        return array('path' => 'node/add/video', 'query' => 'param1=100&param2=200');

How to return users to their original page

If (and only if)

  • you are trying to redirect your users back to where they were after logging in
  • and you take them to "/user" to login (the default login page)

... then you will need to make the "login" link leading to that page to look like:

"/user?destination=<strong>current-page</strong>"

You can do this by creating a custom block and using php there:


global $base_url;
global $user;


$destination = __custom_postproccess_destination_url(drupal_get_destination());

function __custom_postproccess_destination_url ($destination_plus_path_with_query) {
  // urldecode, then remove "destination=", then split with ?-s
  $parts = explode ("?", preg_replace("!^destination=!",  "", urldecode($destination_plus_path_with_query) ) );

  // should be equal to $_GET['q'] from above
  $path_wo_query = $parts[0];

  $query = $parts[1];

  if ($query != "") {$query = "?". $query;}

  $alias = drupal_get_path_alias($path_wo_query);

  return "destination=" . urlencode($alias . $query);
}


// use the $destination var below.

// logged in
if ($user->uid) {

  print '<a href="' . $base_url . '/user/'.$user->uid.'">Profile</a><br />
<a href="'.$base_url . $base_path.'/logout">Logout</a>';

} // not-logged in (= anon)
else {

  print '<a href="' . $base_url . $base_path.'/user/login?' . $destination . '">Login</a><br />
<a href="'.$base_url . $base_path.'/user/register">Register</a>';

}

Drupal 5.x

$_SESSION['login_page'] was added and stores the page you were before clicking login form button. You can use it to determine the page you logged from instead of $_GET['q'] because $_GET['q'] always equals to 'login_redirect'.

Comments

tl731’s picture

Hi. I'm trying to return users to their original page. However, I'm a bit confused by the instructions in general. Please see the following specific questions:

1) Can I infer from this path "/user?destination=current-page" that this must be created on every page that requires login?

2) What do you mean when you say "You can do this by creating a custom block and using php there:" ? Where can I put this?

Any clarification may help. Currently, I cannot return users to their original page.

Thanks.

s4j4n’s picture

Regarding your questions...

1) Can I infer from this path "/user?destination=current-page" that this must be created on every page that requires login?

Yes. Assuming you want the user to be redirected to that page after login.

2) What do you mean when you say "You can do this by creating a custom block and using php there:" ? Where can I put this?

Add a new block. Add php code to the body of that block that does what you need (don't forget to set to allow PHP input format) - see below for more detailed discussion. Put it in whatever region you like.

Regarding the code that is presented above as part of this tutorial...

I went about solving this differently and IMHO I think this is an easier and more general solution:

1. Create a "Login" block and place it in a region of your choice.
2. In the body of the Login block use php and add a link with the URI: "/user?destination=CURRENTPAGE" You could do something like this:

<div class="login">
			<a href=<?php print '"/user?destination=' . check_plain(request_uri()) . '"'; ?>>Login</a>
		</div>

3. Set the "Login" block to show for unauthenticated users only.
4. Create a "Logout" block and place it in a region of your choice.
5. In the body of the logout block use full html and a link with the URI: "/logout" You could so something like this:

<div class="logout">
			<a href="/logout">Logout</a>
		</div>

6. Set the "Logout" block to show for authenticated users only.

Please note that the login code above on MAMP is creating an extra slash, but the URL seems to be working anyway. When I deploy this to the server I assume this will go away. If not, I'll come back and comment.

If you are curious see info on request_uri() and possibly also ltrim()

schajee’s picture

Be careful when using ltrim in the method described above to strip the slashes
echo ltrim(check_plain(request_uri()), '/');
This results in the default redirection to User page when logging in from the site root, as destination becomes an empty string.

And the same ?destination=URI applies to logout as well.

GreenReaper’s picture

Because my theme didn't really work well using an additional block, I ended up modifying the existing link in my theme's template.php instead:

function THEMENAME_menu_item_link($item) {
  if ( $item['href'] == 'user/login' ) {
    $uri = check_plain(request_uri());
    if ($uri != '/') {
      $item['localized_options']['query'] = 'destination=' . ltrim($uri, '/');
    }
  }
  return l($item['title'], $item['href'], $item['localized_options']);
}
asb’s picture

@s4j4n: The modified login link (with check_plain(request_uri()) seems to work well.

Thank you!

klidifia’s picture

Worked well, used it to set logout destination to last page also...

echo "<a href=". $base_url . $base_path ."/logout?". $destination .">Logout</a>";