CodeIgniter Forums
Session Problem in CI4 - 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 Problem in CI4 (/showthread.php?tid=86002)

Pages: 1 2


Session Problem in CI4 - tareq - 12-30-2022

I am trying to setup a Project using CI4. My problem is I can not access Session Data through my Project. Below are my settings:

\App\Config\App Settings are as below:
Code:
public $forceGlobalSecureRequests = true;
public $sessionDriver = FileHandler::class;
public $sessionCookieName = 'fw_session';
public $sessionExpiration = 7200;
public $sessionSavePath = WRITEPATH . 'session';
public $sessionMatchIP = false;
public $sessionTimeToUpdate = 300;
public $sessionRegenerateDestroy = true;

\App\Controller\BaseController Settings are as below:
Code:
namespace App\Controllers;

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.
*/
abstract class BaseController extends Controller
{
    /**
    * Instance of the main Request object.
    *
    * @var CLIRequest|IncomingRequest
    */
    protected $request;

    /**
    * @var \CodeIgniter\Session\Session;
    */
    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.
      $this->session = \Config\Services::session();
      $this->request = \Config\Services::request();
    }
}

\App\Controllers\Login as below:
Code:
namespace App\Controllers;

class Login extends BaseController
{
  /**
    * Class Constructor
    */
    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
    }
 
  /**
    * Default Method of the Class
    *
    * @return void
    */
    public function index()
    {
        $this->session->set("userid", 1);
        $this->session->set("userName", "Demo");
        //many values like this
        var_dump($this->session);  //There is no data. Also on Dashboard no Session data
        return redirect()->to("dashboard")->withCookie();
    }
}

Also WRITEPATH.'session'  folder is empty. Please help me to solve this issue.


RE: Session Problem in CI4 - kenjis - 12-30-2022

The code seems no problem.

Quote:var_dump($this->session);  //There is no data. Also on Dashboard no Session data

What do you mean by no data. What value exactly shows?


RE: Session Problem in CI4 - ozornick - 12-30-2022

What? $request exists by default
PHP Code:
$this->request = \Config\Services::request(); 



RE: Session Problem in CI4 - tareq - 12-30-2022

(12-30-2022, 06:08 PM)kenjis Wrote: The code seems no problem.

Quote:var_dump($this->session);  //There is no data. Also on Dashboard no Session data

What do you mean by no data. What value exactly shows?

There is no data that I set using $this->session->set() command


RE: Session Problem in CI4 - InsiteFX - 12-31-2022

Add this to the top of your BaseController.

PHP Code:
use CodeIgniter\Session\Session



RE: Session Problem in CI4 - tareq - 12-31-2022

(12-31-2022, 12:20 AM)InsiteFX Wrote: Add this to the top of your BaseController.

PHP Code:
use CodeIgniter\Session\Session

Still did not work.


RE: Session Problem in CI4 - kenjis - 12-31-2022

(12-30-2022, 10:15 PM)tareq Wrote: There is no data that I set using $this->session->set() command

I cannot see your screen.
Please show what you got.
You are saying what you don't see, but it does not help a lot.

One thing, you should check:
PHP Code:
var_dump($this->session->get('userid')); 

$this->session is a session object, and if you var_dump it, it does not contain any session data you set.


RE: Session Problem in CI4 - tareq - 12-31-2022

(12-31-2022, 02:12 AM)kenjis Wrote:
(12-30-2022, 10:15 PM)tareq Wrote: There is no data that I set using $this->session->set() command

I cannot see your screen.
Please show what you got.
You are saying what you don't see, but it does not help a lot.

One thing, you should check:
PHP Code:
var_dump($this->session->get('userid')); 

$this->session is a session object, and if you var_dump it, it does not contain any session data you set.

OK. I am explaining it. As you can see, I set userid variable using $this->session->set('userid', 1);
Now on Dashboard I am using var_dump($this->session->get("userid")); which shows me NULL


RE: Session Problem in CI4 - kenjis - 12-31-2022

What if you use dd($this->session->get("userid")) on Login controller?
Does your web server PHP have permission to write in WRITEPATH.'session'  folder?


RE: Session Problem in CI4 - kenjis - 12-31-2022

Try Local Development Server. See https://codeigniter.com/user_guide/installation/running.html#local-development-server
If your code works on it, the problem is in your server configuration.