phpcodeignitersessionsession-variables

How to store variable in session codeigniter


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

In Controller

<?php
session_start(); 
$_SESSION['myvar']='myvalue';
?>

Solution

  • 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 :)