CodeIgniter Forums
CI4: not getting session data in another controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: CI4: not getting session data in another controller (/showthread.php?tid=76404)



CI4: not getting session data in another controller - bhuvnesh.maheta - 05-10-2020

Dear Sir,

In Auth Controller, I have set session data and redirect to Homepage controller but I am unable to get session data in Homepage controller. Please find below code.

In Auth.php (Controller)

$mySess = session();
                    $mySess->set($data);         
//                    $this->session->set($data);

                    return redirect()->to(site_url("homepage"));


In Homepage (Controller)

<?php
namespace App\Controllers;
use CodeIgniter\Controller;


class Homepage extends BaseController
{

    public function index() {
        $mySess = session();
        $sessionArray = $mySess->get();
        print_r($sessionArray);
        exit();
    }
//--------------------------------------------------------------------
}



as a result I am getting blank page.

Please help.


RE: CI4: not getting session data in another controller - cristobal - 04-06-2021

I've the same problem
can you tell me how you solved it?



RE: CI4: not getting session data in another controller - InsiteFX - 04-06-2021

Once you load the session it is loaded for all controllers etc;

Just use.


PHP Code:
// using the helper
$value session()->get('value'); 

I load the session library in the Base controller then it is global to all.

PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Config\Services;
use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Psr\Log\LoggerInterface;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *     class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 */

class BaseController extends Controller
{
    
/**
     * Instance of the main Request object.
     *
     * @var IncomingRequest|CLIRequest
     */
    
protected $request;

    
/**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    
protected $helpers = [
        
'debug',
    ];

    
/**
     * @var \CodeIgniter\Session\Session
     */
    
protected $session;

    
/**
     * Constructor.
     *
     * @param RequestInterface  $request
     * @param ResponseInterface $response
     * @param LoggerInterface   $logger
     */
    
public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        
// Do Not Edit This Line
        
parent::initController($request$response$logger);

        
//--------------------------------------------------------------------
        // Preload any models, libraries, etc, here.
        //--------------------------------------------------------------------
        // E.g.: $this->session = \Config\Services::session();

        // Ensure that the session is started and running
        
if (session_status() == PHP_SESSION_NONE)
        {
            
$this->session Services::session();
        }

    }



I then use the helper with chaining to do what I need but you can call it using

PHP Code:
$this-session->get('item');