2

I am writing a custom user plugin to write the session id and user id on a txtfile(This will be in turn used for some of my perl systems) after the user logins on Joomla. So far i have managed to create xml file and the main php file.

defined('_JEXEC');
jimport('joomla.plugin.plugin');

echo "hello"; //this works as when I login I can see the message on top of the message which makes me believe that the user plugin is installed correctly

// Whatever I try to print in the class, it seems to ignore.
class PlgUserperllogin extends JPlugin
{
    public function onUserAfterLogin($options)
    {
          $user = JFactory::getUser();
          $user_id = $user->get('username');
          $usertype = $user->get('usertype');
          $session = JFactory::getSession();
          $sessionid = $session->getId();
          //write to txt file
          //set cookie for the same
    }
}

I am not sure what is missing. I would really appreciate any help on this.

  • is this joomla 3 or 2.5? you might find the discussion here helpful also. joomla.stackexchange.com/questions/16754/… – jamesgarrett Jan 24 '17 at 23:31
  • Its Joomla 3 . I have tried it. Its just that the action doesn't seem to fire the event. If i print say hello , before the class begins it prints fine on joomla after login, but doesn't seem to recognize anything in the class. I would really appreciate any help this. – Teju Desai Jan 25 '17 at 8:37
  • there could be a few things wrong. 1. is your plugin in the plugins/user/yourpluginname folder? 2. do you open your class like: class PlgUserYourpluginname extends JPlugin 3. put a die() function in your code incase you're just being redirected before seeing any ouput. 4. slightly unrelated, you dont need to get the user object again, it's in the $options. – jamesgarrett Jan 25 '17 at 23:48
  • Yes, I have follwed the structure after reading the documentation and yet i do not undesratnd what i am missing. It just doesn't seem to fire the event not sure why. I will try to use the options array and get the user id and things. – Teju Desai Jan 26 '17 at 7:40
  • After trying various events just to see which event works. onUserLogout works , i am not sure why onUserAfterLogin doesn't work. I am really struggling to understand, why one event on the same class would work and other won't . – Teju Desai Jan 26 '17 at 8:27
2

The class name is case sensitive with Joomla, so make sure your plugin name is correct. For example, if the path of the plugin is:

plugins/user/something/something.php

then you would use:

class PlgUserSomething extends JPlugin

You should also be aware that usertype is deprecated. If you wish to get the group[s] of a user, have a look at another answer I wrote:

https://stackoverflow.com/a/18407692/1362108


So for the actual plugin code, try using the following:

XML:

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.6" type="plugin" group="user" method="upgrade">
    <name>Something</name>
    <author>My Name</author>
    <creationDate>27-01-2017</creationDate>
    <copyright>Copyright (C) 2017</copyright> 
    <license>http://www.gnu.org/licenses/gpl-3.0.html</license> 
    <version>1.0.0</version>
    <description>Something</description>
    <files>
        <filename plugin="something">something.php</filename>
    </files>
</extension>

PHP:

defined('_JEXEC');

class PlgUserSomething extends JPlugin
{
    public function onUserAfterLogin($options)
    {
          // User data
          $user_id = $options['user']->id;
          $groups  = $options['user']->groups;

          $allGroups = '';
          foreach($groups as $group)
          {
              $allGroups .= 'Group Id = ' . $group . ' ';
          }

          // Session data
          $session = JFactory::getSession();
          $sessionid = $session->getId();

          JFactory::getApplication()->enqueueMessage('The user ID is: ' . $user_id);
          JFactory::getApplication()->enqueueMessage('The session ID is: ' . $sessionid);

          // $allGroups is a string containing all the groups
          // Write to txt file
          // Set cookie for the same
    }
}

Of course changing PlgUserSomething to the correct value for you.

  • I will keep in mind about the usertype and use groups instead. I have again double checked my class name to make sure, i am doing the right thing. Like mentioned, i tried to use OnUserLogout event and it works so i am not sure what is going wrong . It just doesn't fire it even if i put die to check whether there is a problem with my event. – Teju Desai Jan 26 '17 at 11:24
  • @TejuDesai - What exact version of Joomla are you using? – Lodder Jan 26 '17 at 11:32
  • I am using 3.6.4. – Teju Desai Jan 26 '17 at 11:55
  • I would really appreciate any help on this.... – Teju Desai Jan 27 '17 at 8:03
  • I'll try replicating the plugin at work today and get back to you – Lodder Jan 27 '17 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.