Redirect users after a successful login

If you use the core login module, redirecting the user to a given page (more precisely, to a menu item) is a breeze. But what if you want to do a trickier thing? For example what if you have a custom component/plugin which has a view with restricted parts, available only for logged in users, and you want the user to remain on the same page after the login (for example from a login box shown in a modal box)?

Tricky, eh? Took me some hours of trial and error, digging around forums and such, but here is the Joomla coding trick which will offer you a simple solution:

The trick is pretty simple at the first sight: what if we use the "&redirect=" URL parameter to do the job? Great idea, but how?

The solution is closer, than you might think.

Let's say the page we want to redirect the user is this:


Do will use it as is? hard-coded? No way!, in Joomla is actually pretty simple to find out the actual URL with this code:

$uri = JFactory::getURI();
$absolute_url = $uri->toString();

Now we will base64 encode it, to be usable in an URL:

$redirectUrl = urlencode(base64_encode($absolute_url));

Little explanation: If you use a string returned by base64_encode like"aW5kZXgucGhwP29wdGlvbj1jb21fcGl6emFwaWU=" in a URL as a parameter for the "$return=" URL parameter be sure to escape it with urlencode. If you do it right, the URL with this parameter will be decoded properly and will redirect the user to the desired page. The entire solution is based on the fact, that the controller.php file in the com_users checks the contents of the 'return' in the JRequest object (the Joomla query access object). If it is set and in the base64 format, it base64_decode(s) it and applies that redirection. Be aware, that '+' is one of the characters used in MIME Base64. Any '+' characters in a URL parameter are converted to spaces during query string parsing.

Now, you can build your final URL to trigger the opening of the login from in a modal box, which will look like this:


And using this, you can build the link to trigger the modal window with the login box and the desired behavior:

<a class="modal" href="/<?php echo $loginURL; ?>"><?php echo JText::_('COM_MYCOMPONENT_LOGIN_MESSAGE')?></a>

Don't forget to add this at the beginning of your view template:


Now you have all set up, you have a link with triggers the modal window with a login box which will keep - after successful login - the user on the same page.

What you say? Simple enough?

The professional company manninghamdentist provides all the information on dentist in bulleen.