Taking control of your Drupal site's login destination

Sean Corrales
Sean Corrales
23
Jul 2009

Taking control of your Drupal site's login destination

comment icon9 comment(s) |

The default Drupal login system is pretty basic: users are taken back to the page they logged in from unless they logged in at http://www.example.com/user. In that case, they're taken to their user profile.  But what if you wanted a user to be redirected upon login to an internal landing page that contained news and information for privileged users? How about giving the user a login prompt when they hit an access denied (403) page? And redirect them to the page they were trying to access prior to login? I'll show you how to do just that using a mix of modules and a few lines of PHP to make it work as expected.

For this how-to, I'm using the login page that can be found at http://www.example.com/user. If you're using a block-based login form, you might have a different result. Before we start, download the Login Destination and Redirect 403 to User Login modules. Upload both modules to your site and enable them.

Enabling Redirect 403 to User Login is all it takes to setup this module. If you want, you can go to admin/settings/r4032login and change the access denied message. And that's it. That's all it takes to give users a login box on any 403 page.

Now go to admin/user/login_destination to configure the login destination page. Your first instinct, if you're wanting to preserve the user's original destination prior to login, would be to check the 'Preserve destination' box. Don't do it. With the current release of Login Destination (6.x-2.3 at the time of writing), it doesn't function as you'd expect. In my experience, my redirect on login worked when 'Preserve destination' wasn't checked but, once I enabled it, the redirect to the landing page wouldn't work.

Select the option "PHP snippet" in the URL destination box and paste the following code in the textarea below. Be sure to switch out DESTINATION with your URL. If you want to redirect to "http://www.example.com/internal-home", for example, you would put internal-home in place of DESTINATION.

if(isset($_GET['destination'])){
       return $_GET['destination'];
} else {
       return 'DESTINATION';
}

In short, this code checks to see if a destination is appended to the URL. If no destination is set, it goes to your default destination. If it is, it takes the user's destination and redirects them there.

Set the radio in "Redirection condition settings" to "Always" and save the configuration. You should now have a site that takes users to the internal home page when logging in from the login form, provides them with the login form when encountering 403 pages, and redirects them to the page they tried to access prior to logging in.

Subscribe to our Networks

Popular Tags by Sean

IW on Facebook

Comments

March 18, 2010

Anonymous

Thanks

Thanks for posting. This was just what I needed.

March 27, 2010

Davey515

very useful, just what i

very useful, just what i needed, thanks

April 29, 2010

Anonymous

Thanks!!

Thanks!!! it works perfectly!

August 3, 2010

llynn

I've been searching for a

I've been searching for a solution to this all morning. Thanks man!

March 14, 2011

paljoo

logintoboggan

Thank you for the idea! Just for those, who already installed logintoboggan (like me) which also can do the 403 redirection:
Similar code can be used, just replace $_GET with $_REQUEST.

May 23, 2011

Banelicious

Words can't express how much

Words can't express how much I love you right now...

June 29, 2011

robin

This is now all rolled into Redirect 403 to User Login module

I needed to implement this on my site and discovered that the current version of "Redirect 403 to User Login" module is now all you need. You don't need the "Login Destination" module or the additional PHP code.

Here is the description of the "Redirect 403 to User Login" module:

Redirect the HTTP 403 error page to the Drupal /user/login page with an optional message that reads:

"Access denied! You must login to view this page." 

Also, the desired page is appended in the url query string so that, once login is successful, the user is taken directly where they were originally trying to go.

December 16, 2012

Eric Lin

Just what I needed, thanks

Just what I needed, thanks

January 9, 2013

Anonymous

great post!

Great post! But I have to ask, why do you still use the login destination module? Doesn't the redirect 403 module take care of all the above?

Search