How does the JSession works, and how to use it in different files?

by ecedenyo   Last Updated August 01, 2019 19:10 PM

I'm using JSession (in Joomla 3.9.x) to implement sort of a captcha field in a form, apparently the captcha code is been saved correctly in the session but I can't find the way to retrieve it, and when I'm trying to find how to use it in the Joomla Documentation, I got to API archived pages (what does "archived" mean?, that they are from older Joomla version?, then how / where do I search for the current version documentation?).

This is the file structure I'm using:

| - views/
| - | - form/
| - | - captcha/

... then in the form (./views/form/tmpl/default.php) I'm calling the captcha in this way:

<img src="<?php echo JURI::root(); ?>index.php?option=my_component&view=captcha&format=raw" width="120" height="40" />

... in the display method of the captcha view (./views/captcha/view.raw.php), after generating the random code I'm saving it this way (following this answer):

$session = JFactory::getSession();
$session->set('myCaptchaCode', $myRandomGeneratedCode);

Then (here it comes!) when I'm testing my session variables in the form (./views/form/tmpl/default.php), just imagine I refresh the form twice, and when I echo:

  • the $_SESSION['myCaptchaCode'], it has the code generated in the previous page;
  • and neither the JFactory::getSession->get('myCaptchaCode') nor the JFactory::getSession->set('myCaptchaCode') has the actual (expected) code, both are empty!

What am I doing wrong, what I'm missing?

Related Questions

Updated August 27, 2016 08:04 AM

Updated January 10, 2017 08:04 AM

Updated April 09, 2016 08:04 AM

Updated June 18, 2017 21:10 PM

Updated November 20, 2018 08:10 AM