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

From Joomla! Documentation

m (update to make the explanation clear)
(Several markup changes. URL corrections.)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[File:Screencapture-Login-module-redirect-dropdown.png|left]]
+
[[File:Screencapture-Login-module-redirect-dropdown.png|right|frame]]
  
To redirect users after a successful login with the 'login module' you must choose a menu item under the 'Options' tab, 'Basic Options' Section.
+
To redirect users after a successful login with the ''login'' module, choose a menu item under the ''Options'' tab, ''Basic Options'' section. Make sure that the menu item selected is a published menu item.
 +
 
 +
To edit a login module you must go to the specific login module:
 +
 
 +
Select {{rarr|Extensions,Module Manager}} and select a ''Login'' module type.
 +
 
 +
The login redirection page is selected from the drop down choices next to the ''Login Redirection Page'' item.
 +
 
 +
From the screenshots you can see the choices of redirection pages are paired to the established menu items. For example, the top menu has the menu items, ''Home'', ''Sample Sites'' and ''Joomla.org'' as choices. The menu items are grouped by the ''[[menu]] type([[alias]])''. Top is called ''top'', User Menu is ''usermenu'' and so on.
  
From the screenshots you can see the choices of redirection pages are paired to the established menu items under each menu. For example, the top menu has the menu items, Home, Sample Sites and Joomla.org as choices. The menus items are grouped by the [[menu]] type([[alias]]), Top is called top, User Menu is usermenu and so on.
 
 
 
[[File:Screencapture-control-panel-menus-dropdown.png]]
 
[[File:Screencapture-control-panel-menus-dropdown.png]]
  
To redirect a user after a successful log in, select the redirection page from the list of menu links offered. Make sure that the menu item selected is a published menu 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''. Choose the ''Logout Redirection Page'' in the same way you would choose the ''Login Redirection Page'' using the drop down choice of menu 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."'' Choose the Logout Redirection page the same way you would choose the Login Redirection page using the drop down choice of menu item.
 
 
{{clear}}
 
{{clear}}
 
==Overriding==
 
==Overriding==
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:
+
If you would like to override the default login specified in your Administrator Backend from your custom code, 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,
+
* 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">
+
 
$redirectUrl = urlencode(base64_encode($redirectUrl));
+
<syntaxhighlight lang="php">
// a base64_encode of index.php?option=com_pizzapie' yeilds: 'aW5kZXgucGhwP29wdGlvbj1jb21fcGl6emFwaWU=' </source>
+
$redirectUrl = urlencode(base64_encode($redirectUrl));
 +
// a base64_encode of index.php?option=com_pizzapie' yields: 'aW5kZXgucGhwP29wdGlvbj1jb21fcGl6emFwaWU='
 +
</syntaxhighlight>
 +
 
 +
{{ambox|image=serious|style=notice|text=
 +
Due to a security fix on Joomla 3.4.6, the redirect URL must be an '''internal''' URL. It must start with ''index.php?'' and be a non-SEF URL.
 +
 
 +
Before Joomla 3.4.6, it worked but that was due to a bug in the way Joomla validated the URLs. Now that security has been applied and the URLs tested correctly the above examples will fail.
 +
}}
  
 
{{ambox|image=serious|style=notice|text=
 
{{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>.
+
If you use a string returned by [https://www.php.net/manual/en/function.base64-encode.php base64_encode] in a URL as a parameter, be sure to escape it with [https://www.php.net/manual/en/function.urlencode.php urlencode]. ''+'' is one of the characters used in MIME Base64. Any ''+'' characters in a URL parameter are converted to spaces during query string parsing. Thus, if you say ''"&return=" . base64_encode("http://localhost/~me")'', it'll be decoded as ''http://localhost/[Y''.
 
}}
 
}}
 +
* Prepend the '&return=' query string to your newly-encoded $redirectUrl. For example:
 +
<syntaxhighlight lang="php"> $redirectUrl = '&return='.$redirectUrl;
 +
</syntaxhighlight>
  
* Prepend the '&return=' query string to your newly encoded $redirectUrl. For example,
+
* Append that $redirectUrl to the Joomla Login URL. For example,
<source lang="php"> $redirectUrl = '&return='.$redirectUrl; </source>
+
<syntaxhighlight lang="php">
* Append that $redirectUrl to the Joomla Login Url. For example,  
+
    $joomlaLoginUrl = 'index.php?option=com_users&view=login';
<source lang="php">
+
    $finalUrl = $joomlaLoginUrl . $redirectUrl; </syntaxhighlight>
              $joomlaLoginUrl = 'index.php?option=com_user&view=login';
 
              $finalUrl = $joomlaLoginUrl . $redirectUrl; </source>
 
  
When you display that $finalUrl, it should look something like this:
+
When you display that ''$finalUrl'', it should look like this:
'index.php?option=com_user&view=login'''&return=aW5kZXgucGhwP29wdGlvbj1jb21fcGl6emFwaWU=''''
+
''index.php?option=com_users&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.
+
The ''controller.php'' file in ''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, ''base64_decode''s it and applies that redirection.
  
<noinclude>[[Category:FAQ]]
+
<noinclude>
 
[[Category:Administration FAQ]]
 
[[Category:Administration FAQ]]
[[Category:Version 1.5 FAQ]]
+
[[Category:User Management FAQ]]</noinclude>
[[Category:User Management]]</noinclude>
 

Latest revision as of 20:01, 22 October 2022

Screencapture-Login-module-redirect-dropdown.png

To redirect users after a successful login with the login module, choose a menu item under the Options tab, Basic Options section. Make sure that the menu item selected is a published menu item.

To edit a login module you must go to the specific login module:

Select Extensions  Module Manager and select a Login module type.

The login redirection page is selected from the drop down choices next to the Login Redirection Page item.

From the screenshots you can see the choices of redirection pages are paired to the established menu items. For example, the top menu has the menu items, Home, Sample Sites and Joomla.org as choices. The menu items are grouped by the menu type(alias). Top is called top, User Menu is usermenu and so on.

Screencapture-control-panel-menus-dropdown-en.png

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. Choose the Logout Redirection Page in the same way you would choose the Login Redirection Page using the drop down choice of menu item.

Overriding[edit]

If you would like to override the default login specified in your Administrator Backend from your custom code, 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' yields: '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_users&view=login';
    $finalUrl = $joomlaLoginUrl . $redirectUrl;

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

The controller.php file in 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, base64_decodes it and applies that redirection.