I am migrating codeigniter 2 to codeigniter 4. I am facing lot of discrepencies. Ex set_userdata. Its not available in codeigniter 4.
how can i use set_userdata in codeigniter4 exactly like codeigniter 2. Please help
I am migrating codeigniter 2 to codeigniter 4. I am facing lot of discrepencies. Ex set_userdata. Its not available in codeigniter 4.
how can i use set_userdata in codeigniter4 exactly like codeigniter 2. Please help
Suggesting you as per CI doc. Initialize session by
$session = \Config\Services::session($config); // $config is optional here
then,
$newdata = [
'username' => 'johndoe',
'email' => '[email protected]',
'logged_in' => TRUE
];
$session->set($newdata);
Or
$session->set('some_name', 'some_value');
Let me try to explain to you the easy way. In CI4 you will need to initialized the session library
In Controller
$session = \Config\Services::session();
$sessionData = [
'username' => 'John Doe',
'phone' => 123545678,
'role' => 'Teacher',
'status' => 'Active'
];
$session->set($sessionData);
To Read the Data it is as simple as
echo $session->get('username');
For More information just visit the documentation
You can simply assign data to the $_SESSION array, as with any other variable. Or as a property of $this->session.
Alternatively, the old method of assigning it as “userdata” is also available. That however passing an array containing your new data to the set_userdata() method.
it looks like you can use the set command with an array.
$newdata = [
'username' => 'johndoe',
'email' => '[email protected]',
'logged_in' => TRUE
];
$session->set($newdata);
or use it like that to set a single value
$session->set('some_name', 'some_value');
Hello if I can understand your question. You want to set a session data.
Here is what you will do to solve this particular issue.
Load the session library by using any these session() or service('session')
$session = session();
$session->set('data_index', $data);
Reference the document CI4 Doc
You can set session value in Codeigniter4 as:
session()->set('session_name','session_value');