So I've got this in a system plugin for event onAfterInitialize -

$app = JFactory::getApplication();    
$referrer     = $app->input->getInt('ref');    
$app->setUserState('com_mycomponent.referrer', $referrer);

And I know it's setting the variable because on that file this works -

//get ref_uid from session variable    
$ref_uid = $app->getUserState('com_mycomponent.referrer', null);    
var_dump($ref_uid); die; 

Also, when I put the getUseState on an external php file for testing, it reads as well -

define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/' ));
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe = JFactory::getApplication('site');
$app = JFactory::getApplication();

//get ref_uid from session variable
$ref_uid = $app->getUserState('com_mycomponent.referrer');


However - when I put that same exact getUserState call in a user plugin for onUserAfterSave, it fails to read the session variable and returns null.

I"m sure the code is firing because I get output while testing, echo this and die that etc.

Anyone have any ideas why this isn't working?

Someone on stackexchange had a similar issue so I tried their suggestion and added this to no avail -

$session = JFactory::getSession();
$session->set('registry',   new JRegistry('session'));

Also worth noting is that getUserState is working fine in my custom component, just not in this onUserAfterSave plugin.

Any help would be greatly appreciated, I'm all out of ideas!

  • Are you trying to use setUserState just after user logs in ?
    – Gaurav
    Mar 29, 2017 at 6:55
  • No, it's after system initialize. "in a system plugin for event onAfterInitialize"
    – omni
    Mar 29, 2017 at 7:29

2 Answers 2


The variable was being set on each page load (onAfterInitialize), so it was setting to null whenever the get var wasn't present. Fixed it with if not null;

     $app = JFactory::getApplication();
     $ref  = $app->input->getInt('ref');
     $session = JFactory::getSession();


         $session->set('referrer', $ref);

So now the session variable only sets if it's not null. Duh Omni!


There is no event called onAfterUserSave - it is called on onUserAfterSave - not sure if this is a typo. If it's not a typo, then I have no idea how an event that doesn't exist got triggered.

  • Sorry, and thanks. That was a typo. Fixed here. I am using onUserAfterSave event.
    – omni
    Mar 29, 2017 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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