When a user requests a page he's not allowed to see, he gets an 'Access denied' page with the message 'You are not authorized to access this page. '.
Sometimes the user has access to the page but he hasn't logged in yet. So to make the user's life easier, you provide him with a login form on the 'access denied' page so he can login immediately and gets redirected to the page he requested originally.
How do you do this?
Start by creating a new Page (node of type Page). Set the input format for the body to 'PHP code'. Start typing the message you want to give to the user, something like 'This page is restricted to logged in users. Please provide your credentials below.'.
You then display the login form my typing the following code below your message (or anywhere you want to put the login form):
If you also want the 'Create new account' and 'Request new password' links under your form you can use the login block by typing the following code instead of the previous:
Voila, your page is done. But you won't be able to see it since you are logged in right now. So to test your page log out and go to your page. If you don't know the drupal path of your page, go to Administer > Content Management > Content to find it.
To finish setting up your page, you have to tell Drupal to use your new page as the 'Access denied' page. You can do this by going to Administer > Site configuration > Error reporting and providing the drupal path of your new page for the 403 page.






Comments
Nice Post
Met you in winnipeg about a year ago, I was the cook helping you with a demo at MTS center. Just wanted to give you heads up on an excellent cook book.Your previous posts were real rubbish, but this is good. This one is brilliant. Your blog is getting really better
What if user is logged in
I was using this solution for quite some time and then found out that this is only working if the user in not logged in at all.
What happens if the user is logged in but does not have the right to access the site he will end up in a loop.
Do you have a solution on this?
Regards
Markus
Alternatively you can just
Alternatively you can just go to Administer > Site configuration > Error reporting and set the 'Default 403 (access denied) page' to: user/login
how to redrect a form
Append ?destination=url where url is the drupal path of your destination (not the absolute address)
Login toboggan
The "Login toboggan" module has an option that allows you choose the login block to be shown on the access denied page. If the user logs in, they will be redirected to the page they were originally attempting to reach (assuming their role has access to that page).
The module also has a number of other enhancements to the login process, all of which can be turned on or off.
Can you provide some more
Can you provide some more information on going about redirecting users after a successful logon from a custom access denied page as described in this post?
Thanks,
Alex
Redirect user
This page was great in helping me create the login block for the access denied page. Now how do I add to the code so that upon logon the user is directed to the page he originally requested?
Thanks,
Alex
Works great
Thanks, Davy.
-Steve
Alternatively you can...
Alternatively you can just go to Administer > Site configuration > Error reporting and set the 'Default 403 (access denied) page' to: user/login
You can also use
You can also use "drupal_goto('user', drupal_get_destination())" within an access check. Adding a nice touch with drupal_set_message() indicating that they are not authorized to view the page would be cool too.
If setup correctly, it should lead the user right back on the page they didn't have access to after logging in.
http://api.drupal.org/api/function/drupal_goto/5
Post new comment