Unable to set session in codeigniter

Created at 15-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.

    app\Controllers<Home>.php

    <?php
    
    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

    app\Views<home>.php

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

    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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.