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

sohelamin's avatar

Is there any way to store a session for only 2/3 request?

Hi,

I want to register a session and want to forget it if there any 2 or 3 http request occurs.

0 likes
2 replies
bobbybouwmann's avatar
Level 88

You can keep two sessions for example. One with the count of request and one with the original session. Then based on the that count you reuse the session or not

if ($request->session()->has('http_count') && $request->session()->get('http_count') > 0) {
    $request->session()->set('http_count', $request->session()->get('http_count') - 1);

    $request->session()->keep(['my_session']);
}

Note: This is the basic idea and I might have something wrong, I just did it out of my head on my phone :P

1 like
sohelamin's avatar

Thanks @bobbybouwmann for the idea.

I was also looking for if this can be done via http header like ThrottleRequests as laravel 5.2 or via caching the limit :)

Please or to participate in this conversation.