Accessing session data outside Joomla

前端 未结 15 1501
小鲜肉
小鲜肉 2020-12-08 03:36

I am trying to run an application outside Joomla (not as a plugin) and I would like to access the logged in user\'s information (userid). I am wondering how should I go abou

15条回答
  •  执念已碎
    2020-12-08 03:51

    First of all you have to provide definition to some joomla's constants(identifiers) as follows:

    define( '_JEXEC', 1 );
    define( 'DS', DIRECTORY_SEPARATOR );
    define( 'JPATH_BASE',$_SERVER['DOCUMENT_ROOT'].DS. basename(dirname(__DIR__)) );
    

    where: JPATH_BASE is represents your site's root directory. It must be correct.

    After than, you have to use key files as follows:

    require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
    

    After than, you have to create an application object and initialize it also:

    $mainframe =& JFactory::getApplication('site');
    $mainframe->initialise();  
    

    [this is optional] If you want to import some other libraries, then you can do this as follows:

    jimport( 'joomla.user.user');
    jimport( 'joomla.session.session');
    jimport( 'joomla.user.authentication');
    

    So the core code for your file is as follows:

    define( '_JEXEC', 1 );
    define( 'DS', DIRECTORY_SEPARATOR );
    define( 'JPATH_BASE',$_SERVER['DOCUMENT_ROOT'].DS. basename(dirname(__DIR__)) );
    
    require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
    
    $mainframe =& JFactory::getApplication('site');
    $mainframe->initialise();
    
    //optional use depend on requirement 
    jimport( 'joomla.user.user');
    jimport( 'joomla.session.session');
    jimport( 'joomla.user.authentication');
    

提交回复
热议问题