Samuel Omopariola
Samuel Omopariola

Reputation: 153

Joomla 4 - How to Store Session Variables

The getSession() function is now deprecated in

Joomla 4

The documentation does not seem to make any reference to this functionality and other resource material just seem few and far between.

I will like to know how to store session variables in Joomla 4 and also wayss of retrieving stored session variables.

For example, when storing session variable in a standard php app, serialize() is used when storing objects, and a call to unserialize() is used to retrieve the object from session.

Sample code to store object

session_start();
$obj = new Object();
$_SESSION['obj'] = serialize($obj);

Sample code to retrieve object

session_start();
$session = unserialize($_SESSION['obj']);

In Joomla prior to Joomla 4

$session = JFactory::getSession();
$session->set('name', "value");

Now you can retrieve this session variable using:

$session = JFactory::getSession();
echo $session->get('name');

How can I perform similar functionality in Joomla 4 and above? Any help will be appreciated.

Upvotes: 0

Views: 405

Answers (1)

Samuel Omopariola
Samuel Omopariola

Reputation: 153

use Joomla\CMS\Session\Session;

Session::start();//if not already started
Session::set('name', 'value');

Upvotes: 0

Related Questions