Question:

Passing variables between a PHP module and a PHP script

Ellie: 6 days ago

I am trying to pass some variable from a PHP module (Sourcerer) to custom PHP script using Sessions method (as posted here (https://stackoverflow.com/a/16778482/3565182)). The method works fine between two PHP scripts but when I tried to implement it between a module and a script, it fails.

Overview of the my method :

Code inside php module :

<?php
session_start();
echo 'Welcome to page #1';
$_SESSION['favcolor'] = 'green';
?>

Code inside php script :

<?php
session_start();
echo 'Welcome to page #2<br />';
echo $_SESSION['favcolor'];
?>

Help would be appreciated.

Answer:
Emery: 6 days ago

You shouldn't use using $_SESSION with Joomla, but instead JFactory::getSession().

Try the following:

Module:

$session = JFactory::getSession();
$session->set('favcolor', 'green');

PHP file:

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