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

johnw65's avatar

Session Dropping

Have been using Laravel for several months now and once an user logs in the application create a session user information with no issue.

Now, I want to keep track of a fiscal year that an user selected and keep it as a session value. The fiscal year value can change, but not often. However, in two cases, it frequently looses the session value.

  1. Case I: when making highly intensive ajax calls like Datatables. I believe the solution for this is using 'post' instead of get. If there is a better solution, please let me know.

2 Case II: When making an insert statement using ajax, it looses the fiscal_year session value. Listed below is my code and if there is a better way, please let me know.



if(Session::has('test_fiscal_year')) {
               	$fiscal_year =  Session::get('test_fiscal_year');
            	}

        	else
					//if the session test_fiscal_year is blank.
             	{
                	$fiscal_year_max = Select from a table.fiscal_year

                	//Session::put('test_fiscal_year', $fiscal_year_max);
                  session(['test_fiscal_year' => $fiscal_year_max]);
                	$fiscal_year =  Session::get('test_fiscal_year');
                	Session::save();              	 
            	}

For Case II, sometimes fiscal year comes back blank. There is a central application that uses fiscal year so is there a better way, maybe passing the fiscal year value instead of setting and getting session value for the fiscal year? The application makes lots of ajax calls.

I've tried used both file and cookie method for storing session value.

Thank you.

0 likes
2 replies
koramit's avatar

I prefer this,


// if the session test_fiscal_year is not initiate yet.

// set $fiscal_year from  Select from a table.fiscal_year

Session::put('test_fiscal_year', $fiscal_year);

// then, make use of $fiscal_year and

// next time it will available at Session::get('test_fiscal_year');

And your api endpoints have verify token exception ? If so maybe the ajax is called when the session already timeout.

I have app that use intensive ajax call too (document autosave stuffs) but I have csrf token in the header and if the session timeout it will get 419.

johnw65's avatar

Koramit, I'll try that, but I believe I used Session::put and the results were the same. As I stated before, I when using DataTables, the session drops. Then when that happens, I generally get a csrf token mismatch afterwards.

Thanks.

Please or to participate in this conversation.