Something that seems to be poorly documented is how to get and set session variables in Joomla. I distinctively recall hunting high and low for some vital guidance on this when Joomla 1.5 first came out (all those moons ago!) and having no luck. Hopefully this article will help someone, at some point, with something.

Joomla stores all session data in the database. It also has a rather useful session object that can be used to manipulate the data.

HOW TO GET A SESSION VARIABLE:

//get the session object
$session =& JFactory::getSession();

//get a session variable
$myvar = $session->get('nameofsessionvariablehere');

HOW TO SET A SESSION VARIABLE:

//get the session object
$session =& JFactory::getSession();

//set a session variable
$myvar = $session->set('nameofsessionvariablehere', 'valuehere');

 

Simple, right?
Now you will understand my frustration years ago when I embarked on a futile quest hunting through wiki after forum after website after Joomla core code just to determine 4 simple lines of code!!!

Enjoy.

October 2, 2009 | Tags: , ,

5

  • Thanks for the article, saved me a lot of research time!!!

    Posted by Raul on December 20, 2009 at 11:21 am
  • Thanks for the info! But the article does not say anywhere, what Set and Get do to the variable!

    Posted by Jon on March 23, 2010 at 8:30 pm
  • The ’set’ function sets the session variable whilst the ‘get’ function gets or returns the session variable. Hope that helps!

    Posted by Pete (Twitter: @theneonhive) on March 24, 2010 at 1:08 pm
  • The information provided is very useful.

    Thanks.

    Posted by Jayanti on April 9, 2010 at 10:16 am
  • merci beaucoup sur l’article

    Posted by ahlam on June 24, 2010 at 10:52 pm
post a comment