Unable to set session in codeigniter

Updated at 14-May-2021, By samar

Unable to set session in codeigniter

With this article, we’ll look at some examples of how to address the "Unable to set session in codeigniter" problem.

If you are unable to set session data in Codeigniter you can follow this code snippet to set the session data and after that, you can display it anywhere in your project’s view and controller’s method using this particular session name.
  • Set session data and display in codeigniter 4

    Set session data to controller's method.


    public function setSessionData(){
        // Initializing session
        $session = session();
        // Adding session data
        $session->set('name', 'Samar');
        // You can also get session data in controller method 
        //echo $session->get('name');
        return view('home');

    Display session data in view file


    <html lang="en">
        <p> You can get session data using session name in any view file </p>
            $session = session();
            echo $session->get('name');

    In Codeigniter 4 you can set session data in the controller's method by initializing the session variable and after that add session data to this session variable by passing the value using the set method. You can access this session data anywhere in your project like the controller's method and in any view file.

Back to code snippet queries related codeIgniter