How to add values in session in Codeigniter

$_SESSION['username'] = 'bestinterviewquestion.com' // It can be use in core PHP
$this->session->set_userdata('username', 'bestinterviewquestion.com'); // It can be done in Codeigniter

We can pass array to store values in session in Codeigniter.

$array = array(
    'username' => 'bestinterviewquestion.com',
    'email' => '[email protected]',
    'url' => 'https://www.bestinterviewquestion.com'
);

$this->session->set_userdata($array);

How to remove values in session in Codeigniter

unset($_SESSION['username']); // It can be use in core PHP
$this->session->unset_userdata('username'); // It can be done in Codeigniter

If we want to remove more values from session then we can use like this

$this->session->unset_userdata($array);  // $array is defined above. It is an array with key & values.

How to get data from session in Codeigniter

$this->session->userdata('username'); // Here we will get username, if it is exists in session.

BY Best Interview Question ON 13 Jan 2019