how to Send message when logout

1,032 views
Skip to first unread message

alikon

unread,
Jul 28, 2012, 2:04:33 AM7/28/12
to joomla-de...@googlegroups.com
hi,all
i've the need under some circumstances  to force user logout , and inform for the reason of logout

i was able to force logout with
$app = JFactory::getApplication();
$app->logout();

but i was unable to display the info   
can someone  help me
thanks

TDZWeb

unread,
Jul 28, 2012, 8:14:55 AM7/28/12
to joomla-de...@googlegroups.com
Seems like a job for a plugin using a combination the event onLogoutUser and $app->enquemessage('haha logged you out! :)'). Give it a try.

alikon

unread,
Jul 29, 2012, 1:01:35 AM7/29/12
to joomla-de...@googlegroups.com
i've already tryied  with the onUserLogout (i'm on 2.5) event with enqueueMessage(),JError::raiseWarning
both no luck at all, user logged out but no message showed

instead using Jerror::raiseError  the message is showed  but the user is not logged out

other ideas ?

TDZWeb

unread,
Jul 29, 2012, 4:22:03 AM7/29/12
to joomla-de...@googlegroups.com
Does it have to be a personalized message or can it be general? I realize that since the app->logout probably wipes the session it forgets any enqueued messages and resets the application. You could try to redirect the user to a generic "You were logged out because of this" page.

alikon

unread,
Jul 31, 2012, 1:35:28 AM7/31/12
to joomla-de...@googlegroups.com
your solution could be acceptable   (Do you mean to redirect  to an article ?)
even if need some extra work  on extension configuration, it needs another parameter the article id 
it was better something like this:
 $app->logout('loggedout for reason: '.$reason)

Arryn Flynn

unread,
Jul 31, 2012, 8:34:59 AM7/31/12
to joomla-de...@googlegroups.com
I agree with TDZWeb.  The plugin method is the best solution; you just need to make it work. Bear in mind that the plugins must return success or the logout sequence will fail.  In fact, the entire plugin chain must succeed for the login/logout to be successful.  Is your plugin event handler returning a success value?  To test this, just disable your plugin and see if you can logout again.

I think $app->enqueMessage only displays when followed by a redirect, e.g., $app->redirect(url, msg)

subtextproductions

unread,
Aug 1, 2012, 3:34:34 PM8/1/12
to joomla-de...@googlegroups.com
I was thinking about this problem and I had an idea along the lines of TDZWeb. Instead of logging the user out and redirecting them to a content page or other generic page, maybe you can redirect the user to another page for further processing. For example, look at this code:

$app->logout();
$app->redirect(JRoute::_('index.php?option=mycomponent&view=mymessagequeue&myvar=1'));

After the application logs the user out and the page reloads, the message queue gets wiped out. So, how about instead set an extra variable in your redirect. Use this extra variable to determine the message you want to display to the end user. Then in your view.html.php file for the mymessagequeue view, add some code like this:

$myvar = JRequest::getInt('myvar');
switch($myvar){
case 1:
  $mymessage = "MESSAGE 1";
  break;
case 2:
  $mymessage = "MESSAGE 2";
  break;
case 3:
  $mymessage = "MESSAGE 3";
  break;
}
$app->redirect(JRoute::_('index.php?option=mycomponent&view=myview', $mymessage);

Here the user is redirected twice after logout. The first time the user is redirected the application's message queue and user session are cleared out. The second time the user is redirected, a message is set for the guest user and the message queue should retain it.

alikon

unread,
Aug 2, 2012, 1:26:29 AM8/2/12
to joomla-de...@googlegroups.com
i'm on a plugin and don't have a component....




TDZWeb

unread,
Aug 2, 2012, 6:03:44 AM8/2/12
to joomla-de...@googlegroups.com
I don't think you would need a custom component. You could try to redirect to an article with:
$app->logout();
$app->redirect(JRoute::_('index.php?option=com_content&view=article&id=ARTICLE ID'));

I sometimes make an 'invisible' system menu (not assigned to any module) with individual articles (often in a category I call 'System') that I use for similar purposes, ie. display system messages. That way you can get prettier/more sensible urls and breadcrumbs.

If you want to have a conditional message you must do something to bridge the logout. It seems to me that subtextproductions 's suggestion is the easiest way to do that. Maybe its time to do the component tutorial? It should have everything you need :)
http://docs.joomla.org/Developing_a_Model-View-Controller_(MVC)_Component_for_Joomla!2.5_-_Part_01

Sam Moffatt

unread,
Aug 2, 2012, 12:15:34 PM8/2/12
to joomla-de...@googlegroups.com
If you had a system plugin you could concievably use it to detect
request URL params for yourself and set the message in the guest
session after the logout however you're perhaps in a worse situation
than if you had a component because it'll add a load to every page
load in the site. The component is perhaps the best way with the
architecture we have right now.

Cheers,

Sam Moffatt
http://pasamio.id.au
> --
> You received this message because you are subscribed to the Google Groups
> "Joomla! General Development" group.
> To view this discussion on the web, visit
> https://groups.google.com/d/msg/joomla-dev-general/-/qehZKGW2IXAJ.
>
> To post to this group, send an email to joomla-de...@googlegroups.com.
> To unsubscribe from this group, send email to
> joomla-dev-gene...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/joomla-dev-general?hl=en-GB.

alikon

unread,
Aug 2, 2012, 2:47:02 PM8/2/12
to joomla-de...@googlegroups.com
so summarizing

  1.  use an article (com_content) for redirect the message 
  2.  use a component (com_custom) for redirect the message 
  1.  detect request URL params for yourself and set the message in the guest session after the logout 
    the  first 
    need some webmaster work could be not suitable for a good extension

    the second
    seems more userfriedly for the end user, more work  for developer

    the third 
    is the most interesting for me cause i don't know how to code it, can you help me  explaining a bit more this


    i would prefer third, the plugin solution 

    do you agree?



    subtextproductions

    unread,
    Aug 2, 2012, 3:40:15 PM8/2/12
    to joomla-de...@googlegroups.com
    The logic still holds true even if you aren't using a component. You can use a plugin to accomplish the exact same goal. The point is that you have to redirect twice. The first time you redirect, all session information is destroyed, so you can't count on any information stored by the User. You need to pass a variable through the URL, that's how information gets passed from page to page when you don't have sessions to fall back on. Then, you need a second script to process the variable you set in the URL. By the way, logout always looks for a variable named "return" in the global $_REQUEST, and expects it to be a base64 encoded URL. If you use this, you don't even have to handle the initial redirect, it will just happen automatically.

    For example, at some point in your code, for whatever reason you have decided to logout the user. This is what your code should look like for the actual logout:

    // SET THE RETURN VARIABLE IN GLOBAL REQUEST BEFORE YOU LOG OUT THE USER
    JRequest::setVar('return', base64_encode('index.php?special_condition='.$x));
    $app->logout();

    After the logout is fired, the system will automatically redirect the user to the URL specified by 'return' in the $_REQUEST. Now, instead of a User plugin, this should be a system plugin, because it will fire every time the page is loaded. We are going to use the onAfterInitialise event because it will give us everything we need but it will take place before any page rendering:

    function onAfterInitialise(){
      $some_condition = JRequest::getInt('special_condition', 0, 'GET');
      switch($some_condition){
      case 1:
        $message = "MESSAGE 1";
        break;
      case 2:
        $message = "MESSAGE 2";
        break;
      case 3:
        $message = "MESSAGE 3";
        break;
      default:
        // DO NOTHING BECAUSE THE SPECIAL CONDITION ISN'T SET
        return true;
      }
      // IF THE MESSAGE WAS SET, REDIRECT AND APPEND THE MESSAGE
      $app = JFactory::getApplication();
      $app->redirect('index.php', $message);
    }

    TDZWeb

    unread,
    Aug 2, 2012, 4:41:09 PM8/2/12
    to joomla-de...@googlegroups.com

    "By the way, logout always looks for a variable named "return" in the global $_REQUEST, and expects it to be a base64 encoded URL. If you use this, you don't even have to handle the initial redirect, it will just happen automatically. "

    This is very useful. I can see making a custom logout plugin myself. Brillant and thx :)

    alikon

    unread,
    Aug 3, 2012, 7:21:55 AM8/3/12
    to joomla-de...@googlegroups.com
    maybe i'm doing something  wrong
    but non msg appear cause never $some_condition have something that differs from 0

    p.s
    i'm on a system plugin
    Reply all
    Reply to author
    Forward
    0 new messages