Welcome Guest, Not a member yet? Register   Sign In
Session Problem in CI4
#1
Exclamation 
(This post was last modified: 12-30-2022, 11:11 AM by tareq.)

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

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?
Reply
#3

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

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

Add this to the top of your BaseController.

PHP Code:
use CodeIgniter\Session\Session
What did you Try? What did you Get? What did you Expect?

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

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

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

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

(This post was last modified: 12-31-2022, 05:39 AM by kenjis.)

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?
Reply
#10

Try Local Development Server. See https://codeigniter.com/user_guide/insta...ent-server
If your code works on it, the problem is in your server configuration.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB