keep session data through ajax calls

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
User avatar
JLinker
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 193
Joined: Mon Nov 19, 2007 2:29 pm
Contact:

keep session data through ajax calls

Post by JLinker » Sat Dec 05, 2015 10:23 am

Hi,

I'm having an issue keeping session data through ajax calls in a custom component.
In a model file of the component, there is a loop updating a session variable:

Code: Select all

$i = 0;
$count = 1000000;
foreach ($tables as $table)
{
        //heavy sql queries here, so the loop takes a while to complete

	//set progress
	$progress = ceil((($i+1)*100)/$count);
	myHelper::updateProgress($progress);
	$i++;
}		


In a helper file, i have 2 functions, to read and write the session variable

Code: Select all

class myHelper
{
	public static function updateProgress($val)
	{
		$session = JFactory::getSession();
		$session->set('progress', $val);		
	}

	public static function getProgress()
	{
		$session = JFactory::getSession();
		return $session->get('progress');		
	}
}
If I var_dump the session var in the updateProgress function, I can see that the value is properly set in the session.
But if I try to get the current value of the var in an ajax call, I get null.

If I use $_SESSION rather than the Joomla! native functions, I can read the values through the ajax call.
Why is the session value not readable through ajax?
Am I missing something or mis using the Joomla native sessions?

Thanks a lot for your answers.
JLinker Facebook Tabs for Joomla, Virtuemart, Kunena, K2, JEvents, Hikashop, J2Store | JLinker Menu Generator | https://www.jlinker.com

User avatar
JLinker
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 193
Joined: Mon Nov 19, 2007 2:29 pm
Contact:

Re: keep session data through ajax calls

Post by JLinker » Mon Dec 07, 2015 1:23 am

Looking at other extensions, it seems necessary to load some joomla core files in "isolated" ajax files before doing any work with the session.

For the record:

/* Required Files */
require_once ( JPATH_SITE . DS . 'includes' . DS . 'defines.php' );
require_once ( JPATH_SITE . DS . 'includes' . DS . 'framework.php' );
require_once ( JPATH_SITE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
require_once ( JPATH_SITE . DS . 'libraries' . DS . 'joomla' . DS . 'database'.DS.'database.php' );

/* Create the Application */
$app = JFactory::getApplication('site')->initialise();

A progress bar system will typically need this in order to function properly if the joomla session is used to store progress data.
It looks like my issue was linked to race condition with the user session and the one probably colliding through the ajax call.

The diagnosis might be wrong but the cure works.
Cheers
JLinker Facebook Tabs for Joomla, Virtuemart, Kunena, K2, JEvents, Hikashop, J2Store | JLinker Menu Generator | https://www.jlinker.com

User avatar
JLinker
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 193
Joined: Mon Nov 19, 2007 2:29 pm
Contact:

Re: keep session data through ajax calls

Post by JLinker » Mon Dec 28, 2015 12:18 pm

Hi everyone,
I'm still having serious issues with JSession through ajax calls.
Until now, I've been avoiding the problem by using $_SESSION, rather than JSession.

The Joomla dev team announced recently that from Joomla 3.4.7, it is strongly recommended to go with JSession. I therefore have to find a solution for the issue raised here.

Please let me recap what the problem is.

1- I send an ajax call using javascript. In the ajax file, I'd like to update a session variable.
2- In the file where the javascript call is located, the session variable is still empty after update

Here is some pseudo code in order to understand the issue:

Code: Select all


//Step 1: in a view, I make an ajax call
call index.php?option=my_component&view=ajax&my_var=1

//Step 2: in the ajax file called, I update a session variable my_var
$my_var = JRequest::getVar('my_var');
$session = JFactory::getSession();
$session->set('my_var', $my_var);	 

//Step 3: then in another view, I check if the session var has been updated
$session = JFactory::getSession();
var_dump($session->get('my_var'));
The problem is that In step 3, my_var is null!
If I do the same using $_SESSION, it works.
What is going on?

Thanks in advance for any help or comment.
JLinker Facebook Tabs for Joomla, Virtuemart, Kunena, K2, JEvents, Hikashop, J2Store | JLinker Menu Generator | https://www.jlinker.com

User avatar
JLinker
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 193
Joined: Mon Nov 19, 2007 2:29 pm
Contact:

Re: keep session data through ajax calls

Post by JLinker » Mon Dec 28, 2015 4:35 pm

similar question never answered here:
http://forum.joomla.org/viewtopic.php?f ... n#p3033328
JLinker Facebook Tabs for Joomla, Virtuemart, Kunena, K2, JEvents, Hikashop, J2Store | JLinker Menu Generator | https://www.jlinker.com

User avatar
saharin
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Mon Oct 11, 2010 8:56 am
Contact:

Re: keep session data through ajax calls

Post by saharin » Tue Dec 05, 2017 10:58 pm

problem is actual


Locked

Return to “Joomla! 3.x Coding”