-
samiul1978
Newbie
-
Posts: 5
Threads: 3
Joined: Dec 2019
Reputation:
0
08-15-2020, 05:40 AM
(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.? }
//--------------------------------------------------------------------
}
-
InsiteFX
Super Moderator
-
Posts: 6,381
Threads: 301
Joined: Oct 2014
Reputation:
236
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 )
-
Avega Soft
Member
-
Posts: 70
Threads: 8
Joined: Jan 2019
Reputation:
2
(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
-
InsiteFX
Super Moderator
-
Posts: 6,381
Threads: 301
Joined: Oct 2014
Reputation:
236
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 $request, ResponseInterface $response, LoggerInterface $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 )
-
Avega Soft
Member
-
Posts: 70
Threads: 8
Joined: Jan 2019
Reputation:
2
(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 $request, ResponseInterface $response, LoggerInterface $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!
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.
|