I'm trying to get the module login destination to redirect to the previous viewed page. I've searched far and wide without success.

For the drupal 7 version of the module there is no setting to automatically redirect to the previous page, and I'm unsure what php to place in the Experts Box.

I'm trying to redirect from this page:
user/login

The site is https if that matters. Hopefully I just overlooked something obvious.

Comments

Hi,

You need to use drupal_get_destination to redirect to the previous page, check http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_get... for more info.

Need help ?
Reach me on skype : sag_13684

Share your Posts, Url, Sites
www.sociopost.com

In this case I am still unsure how to proceed and actually need some hand holding (or a point to a php snippet);

Login Destination says:

If the PHP option is chosen, enter PHP code between

<?php

?>
. It should return either a string value or an array of params that the url($path = '', array $options = array()) function will understand, e.g.
<?php
return array('blog', array('fragment' => 'overlay=admin/config', ), );
?>

But my weak attempt doesn't seem to do anything:

<?php
$destination
= drupal_get_destination();
print
$destination;
?>

It should output a string that login destination can use? But nothing happens.

Can I use drupal_get_destination in this php box without including it on the used to get to the log in page? Or is this url always available to be called upon?

Try this :

<?php
return drupal_get_destination();
?>

Need help ?
Reach me on skype : sag_13684

Share your Posts, Url, Sites
www.sociopost.com

Thanks for your reply.

At least something happens, although it is an error message:

Notice: Undefined offset: 0 in login_destination_drupal_goto_alter() (line 292 of /var/www/webapps/drupal-7.12/sites/all/modules/login_destination/login_destination.module).

You might, instead, want to try the LoginToboggan module, which does (pretty simply and without any need to add PHP) provide this functionality. Indeed, you can have the login form on every page (no redirection when submitting the form), or (if your login form is Captcha-protected, for example) you can set it to provide just the simple link normally provided by Drupal, but return you to the referring page (the original page they were on before clicking on the link — i.e., what you want).

It has a number of other nice options so I definitely recommend trying it out.

Okay, I hadn't tried the Login Destination module before, so I've also just installed it to see if I can replicate your issue. Some observations:
In order for it to work, the setting (on admin/config/people/login-destination/settings) called "Preserve the destination parameter" must be UNchecked (otherwise it overloads any rules created by this module). Take a look at that.
I don't see the PHP/"expert" mode directions, hmmm... Oh, now I see: You need to enable the PHP Filter to be able to use them, of course. That said, an empty call to drupal_get_destination() would best be made by a custom module that provides the login link, otherwise if it's only added on the login page, you would just stay on /user after logging in. I don't think that's what you want. I think there must be a way to get the "referrer" and use it as a redirect parameter, but really, unless you have other reasons to use this particular module, I'd strongly suggest just trying LoginToboggan.

Hope that helps. :-)

Solved it!

LoMo I tried your solution but with no dice initially, but the different google searches and reading about logintobbagon (LTB) managed to provide the pieces of the puzzle that I was missing.

I'll lay them out here in detail in case other people are wondering how to do this (and I can see how this would be a commonly sought feature after login).

I used logindestination (LD) module in the end, I couldn't get it to work with LTB, but I'm sure you can.

The key is getting this :
drupal_get_destination
from the page that you are linking from. So if I'm on the products page and linking to the login page I have to include it on the login link. In my case I have the link in a block. Before I didn't include drupal_get_destination in the link, but now I do. This is how I generate the link in my block:

<?php
global $user;
    if (
$user->uid == 0){
        print
l("Login","user/login",array('query' => drupal_get_destination()));
        }
    else{
    print
l("Logout","user/logout",array('query' => drupal_get_destination()));
}
?>

This is the critical part I was missing

I'm learning php, so I can't clearly explain it, but what I do know is that this tells drupal to get the current page and remember it (more or less).

Then I go to the LD modules configuration and create this rule for login and logout in the Expert field:

<?php
$destination
= drupal_get_destination();
print
$destination;
?>

Since drupal_get_destination is now available it can be output as a string that LD can use to bounce the user back to the page just viewed, which is awesome. Thanks for the help everyone on this thread.

Glad you got that sorted out. That was what I was seeing, too... that you need the drupal_get_destination() call to happen before you go to the login page; and the "login destination" module doesn't seem to do that. I'm not sure why you had problems with the logintoboggan module for this; as far as I can tell, this should be default behavior for login links provided by that module (though I've usually used it to provide a nice "login block" that opens up (changes from link to block in one of the sidebars) when clicked—in that case, it also submits the login form and stays on the page (but of course you can't cache the page if the login form includes any Captcha, etc, which is why you might want the login link that redirects back to the "referring" page.

Anyway, your solution looks sound. I think it should also work just with a "PHP filter" block and no module. I don't think that, for your requirement, you need the LD module at all.

Thanks, This worked really well, just what i was looking for. I found I did not need the Login destination module though.

Thanks very much for posting that code! Here is my variation on it. I'm using it on my access denied page:

<?php
global $user;
    if (
$user->uid == 0){
echo
"Access Denied! To access this content, you probably just need to ";
        print
l("log in","user/login",array('query' => drupal_get_destination()));
echo
" (or ";
print
l("register","user/register");
echo
" if you don't have a login). If you are having problems accessing a page, registering or logging in please ";
    print
l("contact us.","contact");
        }
    else{
echo
"Access Denied! You don't have permissions to access this content. If you feel you should have access to it, please ";
    print
l("contact us.","contact");
}
?>

you can also use tokens for this. You need the token filter module (http://drupal.org/project/token_filter) and also make sure that the token filter is available in the text format you are using:

Create a block and insert this:

<p>If you are already have an account you can <a href="/user/login?destination=[site:current-page:path]">login here</a>.</p>

Hi everyone,

Thanks for this great little snippet! How do I make this into a list though? I need to add the li items somehow but not sure how to do it or where they go amongst the code. This is what I have so far:

<ul class="menu">
<?php
global $user;
    if (
$user->uid == 0){
        print
l("Sign in","user/login",array('query' => drupal_get_destination()));
        }
    else{
    print
l("Logout","user/logout",array('query' => drupal_get_destination()));
}
?>

</ul>

You can see that the output I want is thus:

<ul class="menu">
<li>Sign in</li>
</ul>

Any help is appreciated. Thanks.

Sam.

Hmmm, after a load of trial and error, this seems to work:

<ul class="menu">
<?php
global $user;
    if (
$user->uid == 0) {
        echo
'<li>';
        print
l("Sign in","user/login",array('query' => drupal_get_destination()));
        echo
'</li>';
        }
    else {
        echo
'<li>';
        print
l("Logout","user/logout",array('query' => drupal_get_destination()));
        echo
'</li>';
        }
?>

</ul>

Doesn't seem the most graceful but does result in the desired output. Maybe this will help someone.

Sam.

Drupal usually prefers the Alternative PHP syntax which gets rid of all the curly braces.

http://drupal.org/coding-standards (see "Alternate control statement syntax for templates" on this page.)

... so your code would look something like this instead.

<?php global $user; ?>
<ul class="menu">
  <?php if ($user->uid == 0): ?>
    <li>
      <?php  print l("Sign in","user/login",array('query' => drupal_get_destination()));  ?>
    </li>
  <?php else: ?>
    <li>
      <?php print l("Logout","user/logout",array('query' => drupal_get_destination())); ?>
    </li>
</ul>

It also gets rid of HTML inside php.

Excellent. Thanks for the tip, always new things to learn! :)

However, this generates an error:

Parse error: syntax error, unexpected $end in /var/sites/t/SITENAME/public_html/modules/php/php.module(80) : eval()'d code on line 12

This? http://www.mydigitallife.info/php-parse-error-syntax-error-unexpected-end/

After adding this line to the bottom of my php.ini file in root:

memory_limit = 128M
extension=pdo.so
extension=pdo_mysql.so
short_open_tag = On

...the error persists. Need to test the new code locally to try and work out what the problem is.

Sam.

oops, you need a closing <?php endif; ?> after the last closing </li>

Thanks :)

You mean here:

<?php global $user; ?>
<ul class="menu">
  <?php if ($user->uid == 0): ?>
    <li>
      <?php  print l("Sign in","user/login",array('query' => drupal_get_destination()));  ?>
    </li>
  <?php else: ?>
    <li>
      <?php print l("Logout","user/logout",array('query' => drupal_get_destination())); ?>
    </li>
  <?php endif; ?>
</ul>

Seems strange that endif goes after the else statement? Although I'm pretty new to php. Works fine, although I need to obviously do some more learning!

Sam.

Yep, that's how the alternative syntax works. Glad you got it sorted out! It's also a lot cleaner than all those messy curly braces.

The help is very much appreciated.

Sam.

Thanks for this hint: https://drupal.org/node/1569672#comment-7014554, token filter module is very nice for this.

The token [site:current-page:path] did not work for me, instead i have used [current-page:url:path]. I have looked here: https://drupal.org/node/390482#drupal7tokenslist

This worked for me (for Drupal7)

Add a rule
1) create an event for "User has logged in".
2) Add a condition as you need i.e if you need only a particular user role must be redirected you can add that role in your condition.
3) Add a action system-> page redirect
In the redirect url text area paste this code

<?php
  $path
= $_SERVER['HTTP_REFERER'];
  if (isset(
$_GET['current'])) {            //if (isset($_GET['destination'])) {
   
$path = $_GET['current'];             // $path = $_GET['destination'];
 
}                                               // }
 
echo $path;

?>

4) save and try to log in. It should work.

Regards,
BHARATH KUMAR

Thanks, this method works well for me, without login redirect module!

This was a super clean way of implementing without having to set a url for each possible log in location.

Rules also gives you a few nice options about override url set destinations, which could be useful.