We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Session issue - not storing/not retrieving

I am experiencing weird behaviour with my session adapter. In my application I am using the following:

$di->setShared('session', function () use ($config) {
    $session = new Phalcon\Session\Adapter\Files();
    $session->start();
    return $session;
});

In my controller I have the following code

$this->persistent->searchedPhrase = "test";

This seems to be working only partialy. It does set $_SESSION['searchedPhrase'] however if I want to access it on next request using $this->persistent->searchedPhrase I am getting a null value. There is no problem accessing it through $_SESSION['searchedPhrase']. Another weird issue is that if I use

$this->session->set("searchedPhrase1", "test");

this variable is visible in $_SESSION global only for the current request, it does not get stored for me to access it later. My $session->isStarted() is returning false, but still I am able to access a number of session variables I set in other places... The only way that now seems to be working is to add at the end of my main index.php call to session_write_close()

    echo $application->handle()->getContent();
    session_write_close();

So it seems that something is interrupting saving of session data. What could be the cause?



5.3k
Accepted
answer

to clear cookie of your browser and try again:

$this->session->set("searchedPhrase1", "test");

then:

echo $this->session->get("searchedPhrase1");



9.8k

It seems to be working. Now a question to anyone who may know - since

session_write_close();

worked, I am inclined to leave it there, just in case. This shoudn't present any significant performance issue?