Loading…

How to handle sessions in Codeigniter?

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' => 'info@bestinterviewquestion.com',
    '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.

Suggest An Answer

No suggestions avaliable!

Latest post from CodeIgniter Interview Questions

Ask Question