Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JacDev's avatar

Session not stable through normal Php, only through Javascript

Hello,

I have an application and depending on the url I am flipping a session value. That session value is important to define what should be showed.

Now when the user opens the website it seems like the session is everytime changed (?). If I am using dump(session()->getId()); I am getting a different Session code each time.

After the page is loaded automatically an Ajax call is done to the server. If I use the same code in the Ajax call on the server side, I am getting exactly the same sessionID back everytime the Ajax call gets done. Exactly what I expect. The problem is only that the website and Ajax sessions do not match at all. So the value that gets set by the website is not the one that the Ajax call is reading -> hence resulting into strange output.

The whole website is HTTPS and my settings are: driver = file, lifetime = 1000, expire_on_close = false, encrypt = true, domain = null, secure = true, http_only = true.

In the AppServiceProvider_boot I have the following code: \URL::forceScheme('https');

The request cookies and headers are exactly the same (on XSRF_token, laravel_session, Cookie, etc).

Please help, I have no clue how to continue to fix or troubleshoot this.

0 likes
2 replies
JacDev's avatar

Update: It seems to be going wrong (somehow) in the constructor. If I add the following code to my class:

public function __construct()
{
    dump(session()->getId());
}

That gives a random session everytime, whereas the session in my normal function now stays stable. Do anyone have any clue why the constructing is doing this?

JacDev's avatar
JacDev
OP
Best Answer
Level 1

I have removed the code from the constructor and put it in some other function and now the sessions seem stable. Very odd, but well it seems to work.

Please or to participate in this conversation.