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

DavidPetrov's avatar

Laravel cookie forever expires on session end

So either I don't seem to correctly grasp the concept of cookies and sessions or there's something wrong. What I'm strangely experiencing is the following: I've got a very crucial token I need to save for the user when they log in from a special link containing the token, so for that matter I'm using a cookie and am setting it forever.

Here's the code:

    public function incomingGuest(Request $request, $companyHash)
    {
        try
        {
            $id = decrypt($companyHash);
        }
        catch(\Illuminate\Contracts\Encryption\DecryptException $ex)
        {
            //logger('No company with hash '.$companyHash);
            return redirect()->action('HomeController@index');
        }
        
        $company = \App\Company::find($id);
        session()->put('company', $company); 
        
        Cookie::queue(cookie()->forever('company_id', $id)); //that's the other crucial cookie
        
        return redirect()->action('GuestController@index');
    }
    public function index(Request $request)
    {
        $company = company(); //fetches company from the cookie and several other instances if it's not present there
        
        if(!$company)
        {
            return redirect()->action('HomeController@index');
        }
        
        clearConfiguratorSession();
        Auth::login($company->users->first(), true); //works, don't bother
        Cookie::queue(cookie()->forever('is_guest', true)); //that's a crucial cookie, but it's getting emptied
        
        return view('dogrami.select');
    }
    

However, when the session has been open for longer than its lifetime (which by default is 120 minutes), this cookie is getting emptied alongside the session, so the user can refresh and loses his identification functionality. Is that a desired behaviour and if not, how can I sustain eternal cookies? :D Thanks a lot in advance!

0 likes
2 replies
Tjyoung's avatar

did you ever get this figured out? I'm having same issue. Cookie expires regardless of my settings at end of session

DavidPetrov's avatar

@tjyoung Hey, I'm sorry I'm so late to reply. I also should have posted the solution here, but the problem now is that I forgot exactly what was going on. But since I spent quite some time figuring out, I still have a relatively precise memory of what turned out to cause this behabiour. It was either one of those:

  • I had previously rearranged the sequence of the middlewares that were loaded in config/app.php which had some malicious consequences for the unlogged users of my app; OR
  • sessions and cookies were in some (rather not so intuitive for me back then) way related to the authentication guards, which I also happened to have modified... and perhaps not in the way I desired.

Again, I'm sorry I can't recall the exact technical fix with the details, but I thinj that narrows it down optimally since a year has passed. Wish you good luck! You can share your fix here afterwards.

Please or to participate in this conversation.