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

johnDoe220's avatar

The reason for deleting the session automatically after the post request

This is how I use the session

public function request()
{
		session()->put('price', 1000);
		session()->get('price') // 1000
}

public function verify()
{
		session()->get('price') // null
}

Why does this happen? I have used it many times before and there have been no problems. My question is why the session is deleted automatically in the verify method? It is necessary to know that request is of post type. and verify of type GET

0 likes
14 replies
Snapey's avatar

sounds like sessions just are not being persisted. Can you login on this site?

In the browser dev tools, do you see a session cookie being created?

do you have any sort of cookie blocker in your browser?

johnDoe220's avatar

@Snapey Yes, everything is working well and I have entered the site Yes, it has been created There is no cookie blocker and I have even tested with several other browsers and the problem persists

Snapey's avatar

your verify route also uses the web middleware? Its not a separate api route?

johnDoe220's avatar

@Snapey this just used in web.php

// Pay Controller

Route::post('pay', [CardController::class, 'request'])->name('pay.request');
Route::get('verify', [CardController::class, 'verify'])->name('pay.verify');

Of course, along with auth middleware in construct method.

public function __construct()
    {
        $this->middleware('auth');
    }
johnDoe220's avatar

@Snapey this same code with the same structure works in Laravel 9. That's exactly it. I have tested many different modes, the session is deleted in any case. even with session()->keep('price'); and or reflash

Sinnbeck's avatar

@johnDoe220 If you can recreate it very easily, perhaps you could make a small demo app and put it on github?

johnDoe220's avatar

@Sinnbeck The reconstructions do not work at all, in other words, when the result is return from the request method, there are no more sessions! By any method of reconstruction

Snapey's avatar

make sure your initial request ends properly and returns 200

If you end the request with a dd() or similar then the middleware does not run at the end of the request to persist session to physical storage

johnDoe220's avatar

@Snapey This system, as it is known, is a payment service and verify of the payment amount. When I pass the price statically, everything works correctly and the verification is done successfully. Returns an exception when the price sent is not the same as the price received. Now, when I use the session, the second state is created exactly, the price is sent, but in the verify, because it is null and not the same, the exception returns again.

abolfazlahmadiweb's avatar
Level 1

@johndoe220 It's your fault, not Laravel's. I set up a very small program and you can see it works. Please check your codes carefully. If you use a package, make sure that the sessions are not deleted after the requests.

https://postimg.cc/k2RkCcPj
https://postimg.cc/bSzhN29Z
https://postimg.cc/vccK7zTQ

If you want the source code, request that I put it on Github. As @snapey said, it is unlikely that a bug has occurred at this stage, especially in sessions.

Please or to participate in this conversation.