Welcome Guest, Not a member yet? Register   Sign In
Problem with sessions, stored in files
#1

(This post was last modified: 12-16-2024, 09:02 AM by kabeza.)

I'm having a problem with sessions
I need to store a value in a session so in case user is not logged in. I store this value and after user logs in, I redirect him to the right url etc.

I've made a test controller, associated to 2 routes, to try to debug this problem

Code:
...
public function __construct()
{
  $this->session = \Config\Services::session();
}

public function setCookie($value) {
  $this->session->set('itemId', $value);
  echo "Done";
}

public function getCookie() {
  $userData = $this->session->get();
  $logged = $this->session->get('logged_in');
  $itemId = $this->session->get('itemId');
  echo "<pre>";
  echo "userData:<br/>"; print_r($userData); echo "<br/>";
  echo "logged:<br/>"; print_r($logged); echo "<br/>";
  echo "itemId:<br/>"; print_r($itemId); echo "<br/>";
  echo "</pre>";
}

Then I do some test in my PC
http://localhost/myapp/setcookie/25
and then
http://localhost/myapp/getcookie

All works fine, but when I do the same test on server

https://sub.domain.com/setcookie/25
and then
http://sub.domain.com/getcookie

The output I get is:

userData:
Array
(
    [__ci_last_regenerate] => 1734363353
    [_ci_previous_url] => https://sub.domain.com/setcookie/25
)

logged:
NULL

itemId:


The weird thing is that I can see the itemId being stored in the session file (writable/session) on server, after calling setcookie/25

How can I make this work or debug deeper to find out what's wrong ?

PS: writable/session has the 777 chmod, and www-data:www-data ownership, etc.
Reply
#2

I always initialize my sessions in the initController Method.

PHP Code:
<?php

declare(strict_types=1);

namespace 
App\Controllers;

use 
CodeIgniter\Controller;
use 
CodeIgniter\HTTP\CLIRequest;
use 
CodeIgniter\HTTP\IncomingRequest;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Config\App;
use 
Config\Services;
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;

    /**
    * An array of helpers to be loaded automatically upon
    * class instantiation. These helpers will be available
    * to all other controllers that extend BaseController.
    *
    * @var list<string>
    */
    protected $helpers = [];

    /**
    * Be sure to declare properties for any property fetch you initialized.
    * The creation of dynamic property is deprecated in PHP 8.2.
    */
    protected object $session;

    /**
    * Array for View Data
    * @var array
    */
    protected array $viewData = [];

    /**
    * @param  RequestInterface  $request
    * @param  ResponseInterface $response
    * @param  LoggerInterface  $logger
    * @return void
    */
    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger): void
    
{
        // 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();
        }

        // Below is used for Language Localization
        session()->set('locale'$this->request->getLocale());
}

// Then just use the session helper metnod like this
session()->set('data''values');
$data session()->get('data'); 
What did you Try? What did you Get? What did you Expect?

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

I've tried that way
- initializing in BaseController
- using the session helper to set and to get data

When trying at localhost, it works fine
http://localdev/setc/2501515
http://localdev/getc
[Image: 3ppVsly.jpeg]

But when trying on the server, it doesn't
https://my.domain.com/setc/2501515
https://my.domain.com/getc
[Image: Vs5IQ93.jpeg]

When executed in server the 1st. url to set a cookie (initializing on BaseController, using helper method etc.),
https://my.domain.com/setc/2501515
I notice a file were created. When checking the contents of the file
writable/session/pref_agrvm9fahkgomh7634id3ks16isfqdq7
Code:
__ci_last_regenerate|i:1734521750;prueba_sesion|s:7:"2501515";_ci_previous_url|s:52:"https://my.domain.com/index.php/setc/2501515";

When executing in server the 2nd url to get the cookie/session value,
https://my.domain.com/getc
another file got created in writable/session. When checking the contents of this newer file
writable/session/pref_mfuqd55vf0fnm4ohhk95ng4bc5k0ap34
Code:
__ci_last_regenerate|i:1734521751;_ci_previous_url|s:44:"https://my.domain.com/index.php/getc";

I cannot understand the following:

1. The first file created, contains the value set for "prueba_sesion", etc.

2. Why I'm getting 2 files written, one when calling the url to set a session value, and other when calling the url to get the value previously set

3. Why the ci_previous_url contains index.php when I'm working without index.php, I've setup .htaccess correctly and the urls work without index.php

I'm wondering if the IT infrastructure here may be the cause of this problem, because this server is behind lots of nginx proxies, firewall, etc.
Maybe the browser/server cannot sync well with my session, IP or anything?

Now, let's suppose this infrastructure is the cause. How could I solve this in some way, or maybe with another session class. Would this be possible?

PS:
I've tried setting sessions to work with DatabaseHandler, configured mysql, etc. but the problem still persists.
I've tried different browsers, Chrome, Firefox and Brave, none of these worked when trying the server urls
Reply
#4

Hey, I would to participate to the the improvement of CodeIgniter 4
Reply
#5

It sounds like it may have something to do with the way the server is configured.

Ask your server provider to check on this for you.
What did you Try? What did you Get? What did you Expect?

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

(12-18-2024, 10:55 PM)InsiteFX Wrote: It sounds like it may have something to do with the way the server is configured.

Ask your server provider to check on this for you.

No way I'll be able to get fixed by that side, nothing will change.
I thought it could be a way to override this
Thanks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB