Session getId return error

Permalink
Hi,
I used to write code like this on 5.7+ to get the current session id:
$session = new SymfonySession();
$sessionId = $session->getId();

But in latest version of Concrete5 it returns error. Has something changed regarding session handling in latest version?
Call to a member function getId() on null

 
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
Hi Sepehr,

You can use the Session service instead.
$session = Core::make('session');
$session->getId();
Herkool replied on at Permalink Reply
In relation to this topic, does the session start automatically whenever a user visits my site? Or I should start it manually? Recently I noticed in many cases this `getId` method returns empty string. Are there any known bugs associated with this method?
JohntheFish replied on at Permalink Reply
JohntheFish
Session should start automatically and always be there. The exception may be if a browser has cookies disabled.

There are various session storage engines that sit behind php: files, database, memory etc. Session issues can arise if c5 and environment session handler are not agreeing on configuration.
Herkool replied on at Permalink Reply
Thank you JohntheFish.
But I did a number of tests and I guess the session starts only for logged-in users. I set this code in \application\bootstrap\app.php:
Events::addListener('on_shutdown', function($event) {
    $session = Core::make('session');
    var_dump($session->isStarted());
    var_dump($session->getId());
});

and it always spits out "empty" for not logged-in visitors.

Then I tested this:
Events::addListener('on_start', function($event) {
    $session = Core::make('session');
    $session->start();
});
Events::addListener('on_shutdown', function($event) {
    $session = Core::make('session');
    var_dump($session->isStarted());
    var_dump($session->getId());
});

And it generates the id for every visitor. Does it make sense? Or am I missing something here?
JohntheFish replied on at Permalink Reply
JohntheFish
There should always be a session, even if a guest has cookies disabled there should be a session that lasts as long as the request.

The usual situation is a guest has cookies enabled, so they will have a session cookie set that will be used to associate them with server-side session data between requests.

You shouldn't need to start a session. It will already be started by c5. Perhaps that is what is going wrong, you are starting a new session and it is empty because you just started it.
[edit] though reading again, you are testing the opposite of that.
Herkool replied on at Permalink Reply
So how should i justify the first piece of code? I guess the "on_shutdown" is triggered at the very end of a request cycle. And as I checked, for non-logged-in users, it always returns empty.