3

I'm setting a session variable from external file using ajax:

define( '_JEXEC', 1); 
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__) .'/' ) );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$session    = JFactory::getSession();
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();

// $session->clear( 'ss');
if (isset($_POST['v'])) {
$value = filter_input(INPUT_POST, 'v', FILTER_SANITIZE_NUMBER_INT);
$session->set('ss', $value);
$response = $session->get('ss');
// echo $response;
} else {
    echo $session->get('ss');
}

So with that I'm setting my session var from external file, now the problem I have is retrieving it, say you need to retrieve that session value from a helper file:

public function calculation($carreton) {
// lots of line to make some calculations
$session =& JFactory::getSession();
if ($session->get('ss') == 1) {
    $seg = 1000;
    $subtotal_e = $subtotal + $seg;
} else {
 $subtotal_e = 0;
}
// Final results
return $someArrs;
}

So the IF a = b is working fine the problem is retrieving the values from the session that I have set from external file...

Any idea how to retrieve that value?.. maybe I'm setting it wrong, the file that sets the session is in the root file... domain.com/sess.php but the helper file is in components/com_carreton/helper/file.php

Any ideas?

Thank you.

  • Why do you not make the ajax call to a view in the component? You can do a raw view and the request will arrive with a whole Joomla context. – Laoneo May 22 '14 at 6:05
  • What version of Joomla are you using? – TryHarder Jun 15 '14 at 0:44
3

Your using the wrong session. If you follow the logic down the class chain for $mainframe->initialize you will find, depending on your version of joomla, a line like https://github.com/joomla/joomla-cms/blob/staging/libraries/joomla/application/web.php#L1062

$session = JSession::getInstance($handler, $options);

That session creation call contains the variables in the options tag which define the name of the cookie used for the session.

By calling JFactory::getSession() before initialize you have created a session which most likely has a different cookie name or id number. Instead use

$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
$session    = $mainframe->getSession();
  • Would $session = JFactory::getSession(); do the same thing as $session = $mainframe->getSession(); ?? Or is this also incorrectly starting the session? – TryHarder Jun 15 '14 at 1:05
  • I really can't say, in theory after the application is initialized, both methods should return the same session. Getting the session from the application object ensures you get the same session used by the cms. If the application class was programmed properly, it will set the session property in JFactory to match but I have no confidence that it is. – garyamort Jun 15 '14 at 1:58
  • Thanks. The reason I asked is because I've had session variables go missing (intermittently) in the past. I was using $session = JFactory::getSession(); – TryHarder Jun 15 '14 at 2:55
0

If you turn debug on from the system tab in your global configuration, you will be able to see what you have in the session in the debug panels at the bottom of the screen.

I would avoid using an external file, why not use a function in a controller to do the ajax call instead?

0

Try this!

$session   =& JFactory::getSession();

$mainframe = JFactory::getApplication('site');
  • Please explain your code-only suggestion. – mickmackusa Jul 27 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.