1

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

1

5 Answers 5

1

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');
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

From the doc,

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');

Comments

0

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

Comments

0

You can set session value in Codeigniter4 as:

session()->set('session_name','session_value');

2 Comments

Welrico Dcosta, you should check before edit. Again you add the syntax error.
Can you please enlighten me where is the syntax error??

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.