Welcome Guest, Not a member yet? Register   Sign In
Session in CI 4 is not working accross the controller
#1

(This post was last modified: 08-15-2020, 07:42 AM by jreklund.)

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.?
}

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


Reply
#2

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')); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(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
Reply
#4

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();
        }
    }


What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(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!
Reply
#6

(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.
Reply
#7

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB