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

ibanks's avatar

Session Data Lost Not Sure Why

Hello guys. So there is a part of an application I'm working on where certain pages/urls should not be accessed unless the step that precedes it has been completed. So I created a new middleware class that analyzes the request for these group of specific routes. For some reason though, when I type in a url from one of the completed previous pages/steps, the session data gets lost. Some class where session data is set:

  public function subsidyCheck(Request $request)
  {
     $this->validate($request, [
      'subsidy' => 'required'
     ]);
     $subsidyID = $request->input('subsidy');
     $request->session()->put('subsidyID', $subsidyID);
      ....(etc)....
      return redirect('enroll/personalinfo');
   }

So they advance to the next page. Now let's say I type in the url for the previous step. Here is part of the middleware that is hit:

  public function handle($request, Closure $next)
  {
     ...(some logic)...
     if (in_array($requestedPage, $pageMap))
     {
       return $next($request);
     }
     else
     {
       return redirect('enroll/zipcode');
     }
  }

Then this GET Controller and method is hit but the session data is lost:

  public function selectSubsidyPage(Request $request)
  {
    $state = $request->session()->get('lifeline_app.userState');
     if (empty($state))
     {
      // if there is no state the return back to zip code page
          return redirect('/enroll/zipcode');
     }
    ```
  The state should not be empty or null, but it is and I'm not understanding why.
0 likes
4 replies
ibanks's avatar

Sorry guys. This is a coding error.

JeroenVanOort's avatar

What went wrong?

Just this morning a came across an issue with one of our applications where we needed to call session()->save() manually because there was a redirect following the setting of the session data. I wonder of this is the same kind of problem.

ibanks's avatar

@JeroenVanOort Yeah. I saw that others had a similar kind of problem, but my issue was that the session value that I was looking for on a particular page being redirected to was actually being "pulled" from the session earlier on in the script:

$request->session()->pull('value');

Where I should be using:

 $request->session()->get('value')

It's a rookie mistake. My data is persisting so far through all redirect request now. I haven't had to use session()->save(). I'm testing this custom middleware that redirects based on privileges.

Roni's avatar

@JeroenVanOort, where did you find that session()->save(), and why would you need to use it? Shouldn't the put function be dealing with that? I only ask, because I just ran into the same issue, I've actually been experiencing side effects for a while without knowing why something was failing or adjusting based on other situations. But the session save solved, my problem. The only issue is why would you need to use this?

I'm working on a group project, and I've seen people use the session in other parts of the code without this and it seems to work, I'm trying to identify what could be causing the need for it in my section.

Here is an example, upon refresh you would think this would but it never hit 'success'. Once I added the session save it worked as expected. I don't know but it seems like something is wrong with that. Using laravel 5.1.36

php
if(session()->has('test'))
            dd('success');
        else{

            session()->put('test','testing...');
        }
        session()->save();
        dd(session()->all());

Cheer, and thanks.

-Roni

Please or to participate in this conversation.