Session Codeigniter

Asked

Viewed 1,727 times

3

I have this normal Septssion:

foreach ($nofeatured_prods as $k) {
    $_SESSION['itens'][$k->id] = $k;
}

How do I assign equally, however, in codeigniter?

1 answer

4


Use the method set_userdata() of the IC session library

To assign do:

$var = array('nome' => 'teste');
$this->session->set_userdata($var);

Or else:

$this->session->set_userdata('chave', 'valor');

To recover do:

$this->session->userdata('nome');

Loading

To load this library there are two ways, the first is to call it just where it will be used or by demand and the second let the framework already load it in all requests automatically.

First form:

$this->load->library('session');

Second form:

In the config/autoload.php file look for $autoload['libraries'] and add the element in this array session.

$autoload['libraries'] = array('session', 'database');
  • In this case @rray would be $this->Session->set_userdata($k, ???);

  • I need to set all the products that were listed through this array(), in this case, the $_SESSION worked, but in CI it doesn’t work. Because it’s not native to her, it has to be for $this->Session

  • @Andrébaill $k is name you will fetch the second argument is value. Error occurred?

  • 1

    @Andrébaill I would do so to save an array in session $this->session->set_userdata('produtos', $arr);

  • Yes, in this case I did: $this->Session->set_userdata('items', $nofeatured_prods);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.