简体   繁体   中英

Joomla - framework.php unset my vars

I use the Joomla (1.5.26) authentification in an external PHP web application.

My problem is that the included joomla file "framework.php" unsets any variable previously defined.

// some code
$varfoo = 'toto';

define( '_JEXEC', 1 );
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT']);
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php');
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php');

// authentification code

var_dump($varfoo); // NULL

I could include the Joomla before defining any variable but I would like to know if the behaviour is normal or if I'm doing something wrong.

Thank you

I made a single test file

define( '_JEXEC', 1 );
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT']);
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php');
$varfoo = 'toto';
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php');
var_dump($varfoo); // NULL

Joomla 1.5.x cleans the global variables in the JRequest::clean() method, libraries/joomla/environment/request.php , line 486:

foreach ($GLOBALS as $key => $value)
{
    if ( $key != 'GLOBALS' ) {
        unset ( $GLOBALS [ $key ] );
    }
}

If you really need to keep some of your global variables, you can store them in a static class variable.

class Foo {
    public static $data;
}

Foo::$data = new stdClass();

Foo::$data->bar = 'toto';

require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php');
var_dump(Foo::$data->bar); // 'toto'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM