0
votes

I'm trying to following a youtube tuts for make a login system on CodeIgniter. I was pretty new on php framework and I choose CI for its semplicity. So I have an array where I need to store session data:

$data = array('email' => $this->input->post('email'),
            is_logged_in' => true);

the tutorial use userdata(); function with

    $this->session->userdata($data);

but when he try to print de session array do:

    print_r($this->session->all_userdata($data));

alluserdata(); is deprecated, so I read the doc, and I found out that userdata is a legacy function. I want to learn what I write so I try to modernize the code to be updated and clean for CI3.

is use userdata(); good for CI3? or I have to use $_SESSION[]? or $this->session?

The problems is that if I try to use

$this->session->data

and than:

print_r($this->session->data);

I have nothing in output. I just want to output data array in session so I can understand if it's working. Thank you in advance

EDIT if i use set_userdata() for set session and just print_r($this->session->userdata()); for print session all I have is:

    Array
(
    [__ci_last_regenerate] => 1453313633
    [email] => [email protected]
    [is_logged_in] => 1
)

and not info about browser, time ecc...is that normal?

2

2 Answers

3
votes

Set session data

$newdata = array('email' => $email, 'is_logged_in' => true);
$this->session->set_userdata($newdata);

Get session data

$email = $this->session->userdata('email');
$is_logged_in = $this->session->userdata('is_logged_in');

OR

$dataArray = $this->session->userdata();
2
votes

In Codignitor 3 you can get values from $this->session obejct as:

//Normal PHP    
$name = $_SESSION['name'];    

// IN Ci:    
$name = $this->session->name    

// you can also get by
$name = $this->session->userdata('name');

And whats wrong with your code?

You are using session object as

$this->session->data

This should be

$this->session->userdata

is use userdata(); good for CI3? or I have to use _SESSION[]?

For this, yes it's better to use framework libaray becuase you can manage and reuse your code easily.