CodeIgniter Forums
Session in CI 4 is not working accross the 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: Session in CI 4 is not working accross the controller (/showthread.php?tid=77314)



Session in CI 4 is not working accross the controller - samiul1978 - 08-15-2020

Home.php
PHP Code:
<?php namespace App\Controllers;
use 
Config\Services;
class 
Home extends BaseController
{      public $session;
        function __construct()
        
            
            
if (session_status() == PHP_SESSION_NONE)
            {
                $this->session Services::session();
            }
        }
public function 
index()
{
            $user = array(
                'one','two'
            );
                    
            $this
->session->set('usr',$user);
           return view('welcome_message');
}

//--------------------------------------------------------------------




TestSession.php
PHP Code:
<?php namespace App\Controllers;
use 
Config\Services;
class 
TestSession extends BaseController
{      public $session;
        function __construct()
        
           
            
if (session_status() == PHP_SESSION_NONE)
            {
                $this->session Services::session();
            }
        }
public function 
index()
{
            
            
echo '<pre>';
            print_r($this->session->user); // Not printing the the session data assigned in Home.php PLease help what should i do.?
}

//--------------------------------------------------------------------





RE: Session in CI 4 is not working accross the controller - InsiteFX - 08-15-2020

I load it  in the BaseController.

And access it like this using the session helper:

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

// Save session value using session helper
session()->set('user'$this->user->id);
session()->set($userData);
session()->setFlashdata('error'lang('Auth.notLoggedIn')); 



RE: Session in CI 4 is not working accross the controller - Avega Soft - 08-15-2020

(08-15-2020, 10:37 AM)InsiteFX Wrote: I load it  in the BaseController.

And access it like this using the session helper:

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

// Save session value using session helper
session()->set('user'$this->user->id);
session()->set($userData);
session()->setFlashdata('error'lang('Auth.notLoggedIn')); 
Please, show all code your BaseController . Thank you in advance for your answer


RE: Session in CI 4 is not working accross the controller - InsiteFX - 08-16-2020

Here you go, just remove the helpers and put your own in.

You will always have access to $this->session as long as your controllers extend the BaseController.

PHP Code:
<?php
namespace App\Controllers;

/**
 * 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.
 *
 * @package CodeIgniter
 */

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

class 
BaseController extends Controller
{

    
/**
     * 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 = [
        
'auth',
        
'utility',
    ];

    
/**
     * @var string
     * Holds the session instance
     */
    
protected $session;

    
/**
     * Constructor.
     */
    
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();
        }
    }





RE: Session in CI 4 is not working accross the controller - Avega Soft - 08-17-2020

(08-16-2020, 03:17 AM)InsiteFX Wrote: Here you go, just remove the helpers and put your own in.

You will always have access to $this->session as long as your controllers extend the BaseController.

PHP Code:
<?php
namespace App\Controllers;

/**
 * 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.
 *
 * @package CodeIgniter
 */

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

class 
BaseController extends Controller
{

    
/**
     * 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 = [
        
'auth',
        
'utility',
    ];

    
/**
     * @var string
     * Holds the session instance
     */
    
protected $session;

    
/**
     * Constructor.
     */
    
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();
        }
    }


 Thanks!


RE: Session in CI 4 is not working accross the controller - groovebird - 07-15-2022

(08-16-2020, 03:17 AM)InsiteFX Wrote: Here you go, just remove the helpers and put your own in.

You will always have access to $this->session as long as your controllers extend the BaseController.

This question is old, but if you want access to the session in the constructor, then it is not working. The initController function is called after the constructor is called.


RE: Session in CI 4 is not working accross the controller - Vishwas Gagrani - 08-01-2022

I encountered this problem recently.
For me the reason was using hard-coded url: https://www.abcd.com. While the browser url was without “www” ie. https://abcd.com.
So instead of hard coded url, i used base_url( ) to generate the base-url automatically.
And that solved the session problem, and it is now working in all functions.