Grand Marshal Braev
Grand Marshal Braev

Reputation: 283

CodeIgniter - Check if Session variable exist

In CodeIgniter, checking if an input exists is easy.

Rather than using

$something = isset($_POST['something']) ? $_POST['something'] : NULL;

You can simply do:

$something = $this->input->post('something');

My question is, is it the same with session?

$something = isset($_SESSION['something']) ? $_SESSION['something'] : NULL;

the same with

$something = $this->session->something;

Upvotes: 1

Views: 2757

Answers (2)

Pacio
Pacio

Reputation: 543

Yes, $this->session->something will work just fine.

Just remember to load the library. And read the manual if you like:

Upvotes: 1

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

Check whether session variable is set or not using $this->session->has_userdata('variable'); so try like this..

$something = $this->session->has_userdata('something')? $this->session->userdata('something') : NULL;

if session variable is not set then $something has NULL value otherwise it has session value.

For more see codeigniter session

Upvotes: 1

Related Questions