How can I access my state variables in Joomla modules?

Usually I do it like

$app = JFactory::getApplication();
$variable = $app->getUserState('com_mycomponent.view.filter.variable' ,'default');

But in the module the result will be empty or in this case it will be the default value.

In the module there is a selection field. I need to store the input of the selection, to show the selected value again after submitting the page. Without that, the selection field will be reseted after submitting the page.

1 Answer 1


Your code is correct - I have tested it on a custom module myself, in Joomla 3. Please make sure you have cookies enabled. Try this in your module, and you will see that it works:

$app = JFactory::getApplication();
$app->setUserState('com_mycomponent.view.filter.variable' ,'new state');
$variable = $app->getUserState('com_mycomponent.view.filter.variable' ,'default');
echo $variable;

It should output 'new state' (it does in my test environment). If it does not, it may be that cookies are disabled, session variables disabled on your server, or something else wrong with your installation...

If it does output 'new state', then check to make sure you are setting the state before attempting to get it. See here for more information: http://docs.joomla.org/How_to_use_user_state_variables

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.