How to store variable in session codeigniter

前端 未结 4 865
情歌与酒
情歌与酒 2021-01-05 17:10

I would like to store the variable in the session currently i am tryin like this but not working.

In Controller



        
相关标签:
4条回答
  • 2021-01-05 17:36

    I consider storing my session data in a controller. So this is how you should go about storing and retrieving session data

    1.Use autoload in your config.php files. It saves you time and load time

    autoload['libraries'] = ('session);

    2.In your controller.php create an array to store your session data.

    $new_data = array( 'username' => 'martin', 'email' => 'someone@martin.com', 'user_logged => TRUE );

    $this->session->set_userdata($new_data);

    1. Then this is how to call your session data(create a variable and assign it the value of one of the session data you need):

    $username = $this->session->userdata('username');

    For further reference visit codeigniter user_guide/sessions

    0 讨论(0)
  • 2021-01-05 17:39

    Codeigniter

    set in session

    $newdata = array(
                       'username'  => 'johndoe',
                       'email'     => 'johndoe@some-site.com',
                       'logged_in' => TRUE
                   );
    
    $this->session->set_userdata($newdata);
    

    retrieve from session

    $this->session->userdata('username');
    
    0 讨论(0)
  • 2021-01-05 17:47

    You don't have to set the session the PHP way.

    CodeIgnter has its on Session Class

    Load the session library:

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

    To set session data:

    $this->session->set_userdata('some_name', 'some_value');
    

    To retrieve session data:

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

    To remove session data:

    $this->session->unset_userdata('some_name');
    
    0 讨论(0)
  • 2021-01-05 17:54

    Simple :

    First, load this library

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

    Then, to add some informations in session :

    $newdata = array(
                       'username'  => 'johndoe',
                       'email'     => 'johndoe@some-site.com',
                       'logged_in' => TRUE
                   );
    
    $this->session->set_userdata($newdata);
    

    Next, if you want to get values :

    $session_id = $this->session->userdata('session_id');
    

    And to remove :

    $this->session->unset_userdata('some_name');
    

    A simple search "codeigniter session" could have help you ...

    https://www.codeigniter.com/user_guide/libraries/sessions.html

    Don't forget to upvote and mark as solved if you find this useful :)

    0 讨论(0)
提交回复
热议问题