Redirect Page on Member Logout
I'm trying to set a member logout redirect back to a Contao page instead of the "last page visited" call. In the login module there is a redirect page that can be identified". But at logout, it defaults to "Last Page visited".
Does anybody know how I can set a redirect page for the logout module, and what is the syntax to identify the page I want it to return the logged in member to? In this case I want to load "my-account.html" . I've looked at the syntax in the LogoutModule.php but plugging different syntax into this file isn't working, it keeps going back to "last page visited".
Any advice would be appreciated,
Ernest McDermon
Snellville, GA
Re: Redirect Page on Member Logout
You can always use the "postLogout" hook to do this:
http://www.contao.org/hooks.html#postLogout
It would look something like this:
/system/modules/z_custom_logout/config/config.php
Code:
$GLOBALS['TL_HOOKS']['postUpload'][] = array('CustomLogout', 'redirectLoggedOutUser');
/system/modules/z_custom_logout/CustomLogout.php
Code:
class CustomLogout {
public function redirectLoggedOutUser() {
$this->redirect('/my-account.html');
}
}
I'm not 100% sure the syntax is exactly correct, but I think it is close. Once a user logs out that method will fire and redirect them to the page you specify.
You can find out more about the "System::redirect()" method here, but you can also use the PHP way as well.
http://api.contao.org/Library/System.ht ... odredirect
Re: Redirect Page on Member Logout
This is the code that's in the ModuleLogout.php file in isotope ecommerce, it looks like one of the references you mentioned is in here, I'm just not having any success with the syntax for the redirect, and there really isn't much documentation for isotope ecommerce. Any suggestions?
Code:
/**
* Logout the current user and redirect
* @return string
*/
public function generate()
{
if (TL_MODE == 'BE')
{
$objTemplate = new BackendTemplate('be_wildcard');
$objTemplate->wildcard = '### FRONTEND LOGOUT ###';
$objTemplate->title = $this->headline;
$objTemplate->id = $this->id;
$objTemplate->link = $this->name;
$objTemplate->href = 'contao/main.php?do=themes&table=tl_module&act=edit&id=' . $this->id;
return $objTemplate->parse();
}
// Set last page visited
if ($this->redirectBack)
{
$_SESSION['LAST_PAGE_VISITED'] = $this->getReferer();
}
$this->import('FrontendUser', 'User');
$strRedirect = $this->Environment->base;
// Redirect to last page visited
if ($this->redirectBack && strlen($_SESSION['LAST_PAGE_VISITED']))
{
$strRedirect = $_SESSION['LAST_PAGE_VISITED'];
}
// Redirect to jumpTo page
elseif (strlen($this->jumpTo))
{
$objNextPage = $this->Database->prepare("SELECT id, alias FROM tl_page WHERE id=?")
->limit(1)
->execute($this->jumpTo);
if ($objNextPage->numRows)
{
$strRedirect = $this->generateFrontendUrl($objNextPage->fetchAssoc());
}
}
// Log out and redirect
if ($this->User->logout())
{
// HOOK: post logout callback
if (isset($GLOBALS['TL_HOOKS']['postLogout']) && is_array($GLOBALS['TL_HOOKS']['postLogout']))
{
foreach ($GLOBALS['TL_HOOKS']['postLogout'] as $callback)
{
$this->import($callback[0]);
$this->$callback[0]->$callback[1]($this->User);
}
}
$this->redirect($strRedirect);
}
return '';
}
Re: Redirect Page on Member Logout
Well this isn't specific to Isotope (at least it shouldn't be) since Member functionality is part of the core.
The code did point something out, however -- it does it's own redirect AFTER firing the hook. So its possible its working but then the default redirect fires after it anyway.
Try adding the line "exit;" and see if that prevents this from happening:
Code:
public function redirectLoggedOutUser() {
$this->redirect('/my-account.html');
exit;
}
If not, try seeing if at least the hook is working. Log out as a member after changing the code to this:
Code:
public function redirectLoggedOutUser() {
echo '<h1>LOG OUT HOOK CALLED!</h1>';
exit;
}
Also if you don't already, turn on error messages and see if anything pops up.
Re: Redirect Page on Member Logout
Thanks for the comeback. I don't think we are on the right track to make this work. I'm going to repost this question in a different area focusing on the TL_HOOKS for the frontend/ModuleLogout.php . There doesn't seem to be any documentation concerning this syntax, I'm sure that there is a way to make this work, and if not, then there is a logic error in the the ModuleLogout.php
At the very least, the current Login/Logout form should be able to use the inherent to page redirect on login AND logout with just a mouse click. Right now you have to edit code to get this to work (or not work in this case since there are no comments in the code to show an example of where/how to do this page redirect and both stumbling around on this).
I'm thinking that the login module should have a second page selector for redirect action on logout, which would then be "managed" in the CMS.
What do you think?
Ernest McDermon
Snellville, GA
Re: Redirect Page on Member Logout
I think it's worth requesting as a feature, if nothing else to see what the team's response is.
My opinion -- it doesn't bother me having to hand-code a few things. You're always trying to keep a balance between making things easy to do in the back-end vs. not having a zillion different options to wade through. So every addition I think they consider carefully to make sure its worth it.
The problem is probably in my code, because I've successfully used hooks a bunch of times. Unfortunately I didn't have the time to try it out.
A couple of final things you can try, before I leave you to more capable hands in your repost.
1. Try extending your class (maybe it just doesn't inherit the "redirect" method).
Code:
class CustomLogout extends Frontend
...
2. Try instantiating the System class instead (although this one I have feeling is wrong -- still worth trying)...
Code:
...
public function redirectLoggedOutUser() {
$this->Import('System');
$this->System->redirect('/my-account.html');
exit; // EDIT
}
...
Re: Redirect Page on Member Logout
Actually Brian just made a (extensive) typo. The hook should be postLogout instead of postUpload. Also, the behavior of the postLogout hook is not as described here, so an additional change needs to be added. Below is the code that would work, and also attached that in a zipfile (I renamed files and classnames)
TL_ROOT/system/modules/member_logout/MemberLogout.php
Code:
<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
class MemberLogout extends System {
public function redirectLoggedOutMember(FrontendUser $objUser) {
if ($objUser instanceof FrontendUser) {
$this->redirect('my-account');
}
}
}
*change my-account to whatever this needs to be*
TL_ROOT/system/modules/member_logout/config/config.php
Code:
<?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
$GLOBALS['TL_HOOKS']['postLogout'][] = array('MemberLogout', 'redirectLoggedOutMember');
Alternatives
- Could you not also log out by sending a member to a specific page and add the automatic logout module there? It has a redirect feature.[/*:m:oc4t1hon]
- You can add two login modules and hide one for guests and the other for logged in members. Set the different redirect pages and it should work just the same[/*:m:oc4t1hon]
Re: Redirect Page on Member Logout
Wow, did I really type that?
Sorry about that... Thanks for catching it.
I'm having trouble spotting the difference from the documentation... is it because a backend user can be passed to that as well?
Re: Redirect Page on Member Logout
It states that it is fired for frontend users, but without the extra statement it also redirects for backend users that log out. So it must be a mistake or undocumented feature??
Re: Redirect Page on Member Logout - Solved
Quote:
Originally Posted by Ruud
Actually Brian just made a (extensive) typo. The hook should be postLogout instead of postUpload. Also, the behavior of the postLogout hook is not as described
here, so an additional change needs to be added. Below is the code that would work, and also attached that in a zipfile (I renamed files and classnames)
1. Thanks to all for input and suggestions. I tried the Automatic Logout Module and setup a page as you suggested (my-account-logout.html), replaced the login module with the "automatic logout" module, and was able to set the redirect action back to the "my-account.html" page, I hid this new page from navigation and sitemap, and published it.
I went back to the Account Details page, removed the "Login" module i had inserted there, and inserted a "Logout" link that points to the hidden page "my-account-logout.html", and tested. This works and I wind back up at the page I want members to see on logout, "my-account.html" which has the login etc.
2. Although the intermediate page with the automatic logout works, this just seems counter-intuitive to me as a method to logout. When you are logged in with the automatic login module, it shows a "Logout" button that leaves you on that page after logout but it "hides" any information being displayed.
I think that a "Feature Request" is what is needed here, that the login module would have a second page selector block with a "redirect at logout" page selector.
Default would be "remain on this page" as it exists now, and if you check the page selector for redirect on logout, and designate a page, then the standard login module would have a "Logout" button that would work as the user desires. The login module provides some information about how long you've been logged in, and that's nice to have, but the logout and remain on whatever you happen to be on at logout is confusing in my mind.
3. I have not have time to experiment with the logout hooks code Ruud sent over, but I will give that a try probably tomorrow, and will report back on that. I suspect that it will probably work well also.
I know that basically anything that can be described, can be coded to work in a given platform. Fortunately there are a lot of "smart guys (and gals)" in the Contao community who graciously offer their time and energy to help other members of the community, like me.
Again, thanks for the great suggestions!
Ernest McDermon
Snellville, GA
Re: Redirect Page on Member Logout
Don't forget about the other alternative I mentioned. It has not the counter-intuitivity problem.
But you should place the feature request regardles. The current setup allows to redirect to protected pages which might be ok for members, but a clear problem for guests.