f3-factory / fatfree-core

Fat-Free Framework core library
GNU General Public License v3.0
206 stars 89 forks source link

V4 Session Doesn't Passed Between Pages/Routes #370

Open tohizma opened 11 months ago

tohizma commented 11 months ago

Session doesn't passed between pages/routes. Simple test case:

    public function testpost($f3)
    {
        session_start();
        $_SESSION['globalsessiontest'] = "session from global";
        $f3->set('SESSION.f3session', 'session from f3');
        echo 'POST TEST<br>';
        echo '<form method="post" action="testrcvpost">
            <label for="inputfield">Something:</label>
            <input type="text" name="inputfield" /><br />
            <input type="submit" />
            </form>';
    }

    public function testrcvpost($f3)
    {
        session_start();
        echo 'Received:<br>';
        echo 'Global Session: '. json_encode($_SESSION).'<br>';
        echo 'F3 Session: '. json_encode($f3->get('SESSION')).'<br>';
        echo 'POST fields: '. json_encode($f3->get('POST')).'<br>';
        echo 'Received Cookies: '. json_encode($f3->get('COOKIE'));
    }

Result is:

Received:
Global Session: []
F3 Session: []
POST fields: {"inputfield":"type something"}
Received Cookies: {"PHPSESSID":"rohui88ra03rp9qikt7g47hl7m"}

Chrome Inspect shows that session is correct (match with sending page)

Cookie: PHPSESSID=rohui88ra03rp9qikt7g47hl7m;

Tested using form post and ajax post request.

Environment: PHP Version 8.2.10 F3 version 4 dev

"name": "bcosca/fatfree-core",
"version": "v4.x-dev",
"source": {
     "type": "git",
     "url": "https://github.com/f3-factory/fatfree-core.git",
     "reference": "37c5fdcd77cfd0681df59788315b9b72bc653463"
 }

If same code tested using version 3.8 works normally

Zenger commented 4 months ago

I'm not sure what the cause is but the solution I found so far is not to call new Session(); and set session.auto_start = 1 in php.ini. php will auto start sessions and you can still use normal $f3->get calls on sessions as usual, something changed in php versions and f3 has yet to catch up the session is constantly lost, I've tried memcached, db based and file based sessions they all get lost.

tohizma commented 4 months ago

Found solution, maybe variable assignment is backward. Temporary Fix on base.php line 575

function sync(string $key): ?array
{
      //return $GLOBALS['_'.$key] = &$this->_hive_data[$key]; // original code
      return $this->_hive_data[$key] = &$GLOBALS['_'.$key]; // flip hive assignment
}

Not found any side effects yet