Sessions are not saving : Bug in Laravel core

Published 8 months ago by shahidkarimi

session()->put('currency', $currency);

session()->save();

If I immediately dd the session it shows the new value. But in another place, it shows the old value means not saving. I have been facing this issue with every Laravel project.

I tried with all session drivers, redis, array, file and databse. Anybody, please give a permanent solution of this issue.

Best Answer (As Selected By shahidkarimi)
shahidkarimi

Not working, for sure its a bug. I face this in every project. For the first request, it works. But when the page is refreshed it don't work

Alamin

Can you try something like this

<?php

use Illuminate\Support\Facades\Session;    

class XyController extends Controller{    

public function X()    
{
     Session::put('currency', $currency); /* to set session */
             Session::get('currency');    /* to read session data  */
}     
 }       

 ?>           
shahidkarimi

Not working, for sure its a bug. I face this in every project. For the first request, it works. But when the page is refreshed it don't work

jlrdw
jlrdw
8 months ago (245,730 XP)

If you refresh page ???????? Is anything passed to $currency? A modern web app shouldn't be refreshing a page. Rather on each request go through proper view and route to properly pass the necessary data along.

If you are using ajax, DO NOT use session with ajax.

I use sessions often in ver 5.5, and never had a problem.

shahidkarimi

Once something saved in the session it should be persistent. Yes of course on refreshing it should be there. I am not using ajax. Example: Goto Fiverr.com on the bottom right side there is currency dropdown. Change that and see, the page refreshes and sets that currency in session.

topvillas
$request->session()->put('key', 'value');
jlrdw
jlrdw
8 months ago (245,730 XP)

Where is $currency coming from?

I set a search like

Session::put('dogsearch', $dogsearch);

And I use it after an edit to return to page I left off on (part of query string). 100% works.

You are some how passing a variable at the wrong time / place, thus the session isn't properly updating.

@topvillas the static method also works 100% for me. Taylor uses __callStatic().

shahidkarimi

$currency is coming from form input field. $currency = Input::get('currency')

jlrdw
jlrdw
8 months ago (245,730 XP)

And it works if a form is submitted, right?

Snapey
Snapey
8 months ago (998,115 XP)

sessions work fine, but only if your code exits normally and runs the terminable middleware. If you dd() in your code then the session is not saved

https://laravel.com/docs/5.5/middleware#terminable-middleware

you should also run php artisan route:list and check that you have web against each route, once only per route

Please sign in or create an account to participate in this conversation.