# Joomla session get function return old values and don't update with new value

Apologize for my English. I have a boring and very vital problem with Joomla! Session. When a table of data appears on the first page, a series of information is stored in the session as follows:

$session = JFactory::getSession();$session->set('userRecog', $user->id);$session->set('qmanageIdsArray', $qmanageIdsArray);$session->set('userAccessLevel', $userAccessLevel);$session->set('qbookIdArr', $qbookIdArr);$session->set('quserIdArr', $quserIdArr);  In another page I want to get session data with new values as follow: $session = JFactory::getSession();
$userRecog =$session->get('userRecog');
$qmanageIdsArray =$session->get('qmanageIdsArray');
$userAccesslvl =$session->get('userAccessLevel');
$qbookIdArr =$session->get('qbookIdArr');
$quserIdArr =$session->get('quserIdArr');


When new data is merged with the table and reload the page, session set with new tables data. Then I want to get stored session data but the data stored in the old session appears. I have to navigate the page for storing new session values of the new data table. refreshing the page, clearing session, destroying the session, logging out and log in again user, and changing browser doesn't resolve the problem. This problem has taken me all the time for 3 days and I've used any solution, but the problem is still there. Can anyone help me?

• 1. If you get old values, that means that the new values are not written to the session. 2. You write "I have to navigate the page for storing new session values". If you only write into the session on one of the pages, then you can not expect the values to change from another page. 3. You did not specify by which process data gets merged. If the data is not merged by the current user, then there is no reasonable way to notify the session of the user. – Javatasse Oct 30 '18 at 19:17
• according to my reply in @mickmackusa answer (below), I have a datatable with many pages and session work at all pages except first page. in the first page session randomly work and sometimes get old values of the first page. – wahid anvary Oct 30 '18 at 19:41
• after of many tests, I realize that the session sometimes not set with new values for newly fetched query data from MySQL. – wahid anvary Oct 30 '18 at 19:49
• That usually means that the execution of the program doesn't pass the lines of code where you set new values. Maybe the SQL query causes an error and the lines get skipped. Hard to guess without a look at the code. – Javatasse Oct 30 '18 at 20:02
• I tested and do all solution very much. I am hanging on this problem. problem is in the get function of joomla session class. It sometimes get new values sometimes not. that is working until one week ago but after that this error accurs. – wahid anvary Oct 30 '18 at 20:40

I found a solution by changing the Session Handler in the Session Settings at Administrator->Global Configuration from Database to PHP. Now my code works correctly. But I still don't know why the error occurs with Database handler.

• I am not sure, but you call the Application->close() after your Json call and I think this one is calling the exit()-function. So on the database session the data might get not be written to the database, but on the file system sessions the PHP module might still take care of it. I usually make the ajax call with format=raw and just return after the function. – Javatasse Oct 31 '18 at 1:20
• @Javatadse. By the way I checked this issue. That is randomly work when handler is Database. I tried get the seted values from anathor pages without tmpl=component and still Session's get function returned old values. I think this is joomla core side problem. – wahid anvary Oct 31 '18 at 4:38
• It is possible it is a general problem. Some errors are hard to track. Most likely a certain order of page calls will cause a problem like this, but it may be hard to figure out what that order is. In some rare cases it is not the script itself, but I doubt that this is the case here. I usually track the calls and if the execution works as expected and try until I get the error. That can give you a hint where things start going wrong. – Javatasse Nov 1 '18 at 0:08

You could write a condition that checks the session for a userRecog key.

$session = JFactory::getSession(); if ($session->has('userRecog')) {
echo "Old";
// don't set with old data
} else {
echo "New";
$session->set('userRecog',$user->id);
$session->set('qmanageIdsArray',$qmanageIdsArray);
$session->set('userAccessLevel',$userAccessLevel);
$session->set('qbookIdArr',$qbookIdArr);
$session->set('quserIdArr',$quserIdArr);
}


Subsequent page reloads will not engage the initial battery of set() calls.

• Thanks @Lodder bad copy paste from my testing script. – mickmackusa Oct 30 '18 at 8:32
• @Lodder my suggestion feels mildly hacky. Is there a cleaner feeling way to check if the session hasn't yet been loaded with data? A way to call empty() on something? I investigated isNew() but that wasn't doing what I wanted. I'm happy to upvote a more refined answer. – mickmackusa Oct 30 '18 at 8:59
• What you've done seems fine. You can also do if (isset(\$session->get('userRecog'))) { – Lodder Oct 30 '18 at 9:08
• @mickmackusa thanks for suggesting, but i want each time table data reload userRecog and other valuse set again for avoid client side injectin data.i use dynamic datatable with ajax json. – wahid anvary Oct 30 '18 at 11:24
• this problem randomly occurs, sometimes work perfectly sometimes not :(. it occurs only the first page of my data table and when occurs only one way resolve that, it resolves only by data table page navigate (forward next page and back to the first page). when it occurs destroying and other force work doesn't resolve problem and session in the first page of data table return old first page values. – wahid anvary Oct 30 '18 at 14:18