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

Nervmind's avatar

Session problem

Hi guys,

I have problem with my session request.

I have multi step form, everything works fine expect one thing.

When i will submit form in step-1, site is going redirect me to step-2. I have everything in step-2 what i submited in step-1 so it works, but when i go back with arrows to step-1 in e.g Chrome, session disapears.

How can i fix it?

0 likes
8 replies
Nervmind's avatar

@DOUGLASAKULA -

    public function Step1(Request $request)
    {
        $complaint = $request->session()->get('complaint');


        return view('complaint.step1',compact('complaint', $complaint));
    }


    /**
     * Post Request to store step1 info in session
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postStep1(Request $request)
    {
      $validatedData = $this->validate($request, [
        'name' => 'required',
        'surname' => 'required',
        'parcel_number' => 'required',
        'email' => 'required|email',
        'phone' => 'required',
        'loss' => 'required'
      ]);




    $store = Array();
        $store['parcel_number'] = $request->parcel_number;
        $store['name'] = $request->name;
        $store['surname'] = $request->surname;
        $store['email'] = $request->email;
        $store['phone'] = $request->phone;
        $store['loss'] = $request->loss;



        if(empty($request->session()->get('complaint'))){
            $complaint = new Complaint();
            $complaint->fill($store);
            $request->session()->put('complaint', $complaint);
        }else{
            $complaint = $request->session()->get('complaint');
            $complaint->fill($store);
            $request->session()->put('complaint', $complaint);
        }
        return redirect('/step-2')->with(compact('complaint'));
    }

    /**
     * Show the step 2 Form for creating a new product.
     *
     * @return \Illuminate\Http\Response
     */

    public function Step2(Request $request)
    {
        $complaint = $request->session()->get('complaint');

        return view('complaint.step2',compact('complaint', $complaint));
    }
munazzil's avatar

You should change step2 function as like below you have used it as with method in your compact.

   public function Step2(Request $request)
     {
    $complaint = $request->session()->get('complaint');

    return view('complaint.step2',compact('complaint'));
   }
Nervmind's avatar

@MUNAZZIL - This doesn't solve my problem. Is it any solution for this? Why my session disappears when I go back from Step2 to Step1? I can see on laravel debugbar in step2 that session exists with data but when i go back to step 1 debugbar doesn't show data from session.

Nervmind's avatar

Session also dissapears when i will refresh site in step 2.

kerrin's avatar
kerrin
Best Answer
Level 9

Update to save the model after you've filled

if(empty($request->session()->get('complaint'))){
            $complaint = new Complaint();
            $complaint->fill($store);
            $complaint->save();
            $request->session()->put('complaint', $complaint);
        }else{
            $complaint = $request->session()->get('complaint');
            $complaint->fill($store);
            $complaint->save();
            $request->session()->put('complaint', $complaint);
        }
1 like
munazzil's avatar

Check like below xampp/php/php.ini folder path

    Set your php.ini for:

      session.gc_maxlifetime - Default 1440 secs - 24 mins

      session.cookie_lifetime - Default 0
Illas's avatar

As others have pointed out, you need to store the data somewhere. If you refresh a page it will lose the POST data as it will not be doing a POST request anymore. I am assuming this as the method is named "postStep1". Alternatively you can encode the posted data and add it to your URL which you can retrieve even with a GET request.

Please or to participate in this conversation.