Difference between revisions of "How to access session variables set by an external script"

From Joomla! Documentation

(Tagged/Marked for t9n)
(Marked this version for translation)
Line 1: Line 1:
 
<noinclude><languages /></noinclude>
 
<noinclude><languages /></noinclude>
<translate>Situation: when you call a session variable in Joomla from an external script, it appears to be empty.</translate>
+
<translate><!--T:1-->
 +
Situation: when you call a session variable in Joomla from an external script, it appears to be empty.</translate>
  
  
<translate>Solution:
+
<translate><!--T:2-->
 +
Solution:
 
Replace <tt>session_start();</tt> in your external script with</translate>
 
Replace <tt>session_start();</tt> in your external script with</translate>
 
<source lang=php>
 
<source lang=php>
Line 14: Line 16:
 
$mainframe->initialise();
 
$mainframe->initialise();
 
</source>
 
</source>
<translate>Be sure to change <tt>JPATH_BASE</tt> to suit your directory structure.</translate>
+
<translate><!--T:3-->
 +
Be sure to change <tt>JPATH_BASE</tt> to suit your directory structure.</translate>
  
<translate>Replace the <tt>$_SESSION[ 'name' ] = "value";</tt> in your external script with</translate>
+
<translate><!--T:4-->
 +
Replace the <tt>$_SESSION[ 'name' ] = "value";</tt> in your external script with</translate>
 
<source lang=php>
 
<source lang=php>
 
$session = JFactory::getSession();
 
$session = JFactory::getSession();
Line 22: Line 26:
 
</source>
 
</source>
  
<translate>Now you can retrieve this session variable using:</translate>
+
<translate><!--T:5-->
 +
Now you can retrieve this session variable using:</translate>
 
<source lang=php>
 
<source lang=php>
 
$session = JFactory::getSession();
 
$session = JFactory::getSession();
Line 28: Line 33:
 
</source>
 
</source>
  
<noinclude><translate>[[Category:Development]]</translate></noinclude>
+
<noinclude><translate><!--T:6-->
 +
[[Category:Development]]</translate></noinclude>

Revision as of 13:10, 8 June 2016

Other languages:
English • ‎Nederlands • ‎español • ‎français

Situation: when you call a session variable in Joomla from an external script, it appears to be empty.


Solution: Replace session_start(); in your external script with

define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../..' ));

require_once ( JPATH_BASE. '/includes/defines.php' );
require_once ( JPATH_BASE. '/includes/framework.php' );
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();

Be sure to change JPATH_BASE to suit your directory structure.

Replace the $_SESSION[ 'name' ] = "value"; in your external script with

$session = JFactory::getSession();
$session->set('name', "value");

Now you can retrieve this session variable using:

$session = JFactory::getSession();
echo $session->get('name');