How to check if a session exists (Codeigniter)

Asked

Viewed 2,051 times

2

I have the following function in my Controller:

private function SetImageAndColor($client_id) {
    if (isset($_GET['color']) AND isset($_GET['image'])) {
        $dados['click2call'][$client_id]['image'] = $this->input->get('image');
        $dados['click2call'][$client_id]['color'] = $this->input->get('color');
        $this->session->set_userdata('click2call', $dados);
    }
}

It takes the parameters passed via $_GET and saved in session.

My question is this: how do I verify that this session exists, but within the View?

1 answer

4


You can check if there are values in the session:

Example:

// index_view.php
<html>
...
    <?php if($this->session->userdata('color') == 'blue') echo 'do_something'; ?>
...
</html>

However, I did not use this structure in the IC to set values in the session: In your case, you would do as follows, in the manual of Codeigniter:

<?php 
    //controller.php    

    ...

    $image = $this->input->get('image');
    $color = $this->input->get('color');
    $this->session->set_userdata(array('nome' => 'click2call', 'id' => $cliente_id, 'image' => $image, 'color' => $color)); 
  • 1

    Thanks, man! You solved!

Browser other questions tagged

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