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));
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?