Difference between revisions of "How do you redirect users after a successful login?"

From Joomla! Documentation

m (clean up categories with <noinclude> tags)
(2 intermediate revisions by 2 users not shown)
Line 6: Line 6:
 
* Take the url that you would like to redirect the user to after they have successfully logged in and apply the 'base64_encode' function to it. For example,  
 
* Take the url that you would like to redirect the user to after they have successfully logged in and apply the 'base64_encode' function to it. For example,  
 
<source lang="php">
 
<source lang="php">
$redirectUrl = base64_encode($redirectUrl);   
+
$redirectUrl = urlencode(base64_encode($redirectUrl));   
 
// a base64_encode of index.php?option=com_pizzapie' yeilds: 'aW5kZXgucGhwP29wdGlvbj1jb21fcGl6emFwaWU='  </source>
 
// a base64_encode of index.php?option=com_pizzapie' yeilds: 'aW5kZXgucGhwP29wdGlvbj1jb21fcGl6emFwaWU='  </source>
 +
 +
{{ambox|image=serious|style=notice|text=
 +
If you use a string returned by [http://php.net/manual/en/function.base64-encode.php base64_encode] in a URL as a parameter, be sure to escape it with [http://www.php.net/manual/en/function.urlencode.php urlencode].  <code>'+'</code> is one of the characters used in MIME Base64.  Any <code>'+'</code> characters in a URL parameter are converted to spaces during query string parsing.  Thus, if you say <code>"&return=" . base64_encode("http://localhost/~me")</code>, it'll be decoded as <code>http://localhost/[Y</code>.
 +
}}
 +
 
* Prepend the '&return=' query string to your newly encoded $redirectUrl. For example,
 
* Prepend the '&return=' query string to your newly encoded $redirectUrl. For example,
 
<source lang="php"> $redirectUrl = '&return='.$redirectUrl; </source>
 
<source lang="php"> $redirectUrl = '&return='.$redirectUrl; </source>
* Append that $redirecturl to the Joomla Login Url. For example,  
+
* Append that $redirectUrl to the Joomla Login Url. For example,  
 
<source lang="php">
 
<source lang="php">
 
               $joomlaLoginUrl = 'index.php?option=com_user&view=login';
 
               $joomlaLoginUrl = 'index.php?option=com_user&view=login';
               $finalUrl = $joomlaLoginUrl . $redirecturl; </source>
+
               $finalUrl = $joomlaLoginUrl . $redirectUrl; </source>
  
 
When you display that $finalUrl, it should look something like this:
 
When you display that $finalUrl, it should look something like this:
Line 20: Line 25:
 
The '''controller.php''' file in the '''com_user''' 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.
 
The '''controller.php''' file in the '''com_user''' 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.
  
[[Category:FAQ]]
+
<noinclude>[[Category:FAQ]]
 
[[Category:Administration FAQ]]
 
[[Category:Administration FAQ]]
 
[[Category:Version 1.5 FAQ]]
 
[[Category:Version 1.5 FAQ]]
[[Category:User Management]]
+
[[Category:User Management]]</noinclude>

Revision as of 15:42, 1 September 2012

Select the redirection page from the list of menu links offered. Make sure that the link is to a published item.

Note: The same procedure is used for redirecting users on successful logout except you enter the page where you want to redirect successful logouts to where it says "Logout Redirection URL."

If you would like to override the default login specified in your Administration back end from your custom code you can do so with the following:

  • Take the url that you would like to redirect the user to after they have successfully logged in and apply the 'base64_encode' function to it. For example,
$redirectUrl = urlencode(base64_encode($redirectUrl));  
// a base64_encode of index.php?option=com_pizzapie' yeilds: 'aW5kZXgucGhwP29wdGlvbj1jb21fcGl6emFwaWU='
  • Prepend the '&return=' query string to your newly encoded $redirectUrl. For example,
 $redirectUrl = '&return='.$redirectUrl;
  • Append that $redirectUrl to the Joomla Login Url. For example,
              $joomlaLoginUrl = 'index.php?option=com_user&view=login';
              $finalUrl = $joomlaLoginUrl . $redirectUrl;

When you display that $finalUrl, it should look something like this: 'index.php?option=com_user&view=login&return=aW5kZXgucGhwP29wdGlvbj1jb21fcGl6emFwaWU='

The controller.php file in the com_user 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.