Tuesday, July 17, 2012

Joomla 2.5: set session inside & get session outside


We have a need to set a session in joomla extension (e.g. component) and get (read) session in a external file  (outside joomla).

Here is the sample code:

//set session in a joomla extension
$session =& JFactory::getSession();
$session->set('asession', 'avalue');


//get session in a external file (outside joomla)

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/..' )); //should change to match to your file directory
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

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

2 comments:

  1. This does not work in Joomla 2.5 with $mainframe gone.

    ReplyDelete
  2. I have tested on Joomla 2.5, this code works.

    ReplyDelete

Subscribe to RSS Feed Follow me on Twitter!