Redirect users to the login form if access is denied

Out of the box Drupal will display a simple Access Denied message is a user is trying to access content that they do not have permission to. This is not exactly user friendly. A better solution is to redirect the user to the login form and when the user logs in, redirect them back to the content that they want to see. There are two main ways to achieve this.

Option 1: Add some code to page.tpl.php

The first option is to add a simple block of code to the very top of your page template file (page.tpl.php). The code is:

<?php
if ($title == 'Access denied') {
header( 'Location: /user?destination=' . substr ($_SERVER['REQUEST_URI'],1 ) );
} ?>';

The $title variable is the meta title for the page. This would normally be "Access denied" if a user does not have permission to view the content. The if statement simply says, if the meta title is equal to "Access denied" then redirect the user to the user login form (sitename.com/user). It will also send the page that the user is trying to access. Therefore, once the user has logged in, he/she will be redirected back to that page.

This is a nice and simple way of achieving our goal. However, there is one problem with it. If you have caching switched on, the site may cache the access denied message if a user does not remember his/her correct login details. If that happens, subsequent users will get the access denied message.

Option 2: Logintobogan

The second option is to install the Login Toboggan module. The Login Toboggan module will display the login form for Access denied pages (must be set on in the settings). This option works with caching. The Login Toboggan module does other nice things as well, like allowing users to login with their email address instead of username.

If you liked this, you'll love my book, Master Drupal Module Development.

"..the must have drupal developers book"


Feeling stuck with Drupal 8 module dev?

Get the free 7 lesson course that will help you get started today without feeling overwhelmed.

  • Create Drupal modules with just a few commands using the Drupal Console
  • Create custom pages
  • Create custom blocks
  • Create admin forms
  • Demystify routers and controllers
  • Bonus material

Find out more


Comments

Thanks for this tidbit! The "access denied" error is not user friendly at all! Always better to keep the visitor's experience positive.

How to redirect the content of the form to our Email address, when the user clicks the submit button

plz give me the code

That is one of the most simple and helpful things I have found for drupal. Thanks a million

You can do this by going to Administer /Site configuration > Error reporting and providing the drupal path of your new page for the 403 pag, insert your logging url.

@Macks - If you're going to comment on an old thread almost 4 years later, you might want to note that you're referring to Drupal 7, which wasn't even released when this article was originally posted, or even as of the date of the most recent comment (2010) before yours (2012).

Blair Wadman's picture

I've updated the above comment to reference the correct name, rather than Anonymous. There was a bug in the system that was labeling every comment with Anonymous - fixed now!

Add new comment