Accessing session data outside Joomla

前端 未结 15 1335
小鲜肉
小鲜肉 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:49
     define( '_JEXEC', 1 );
    
     define('JPATH_BASE', 'your joomla basedir goes here' );
    
     define( 'DS', DIRECTORY_SEPARATOR );
     require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
     require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
    
     JDEBUG ? $_PROFILER->mark( 'afterLoad' ) : null;
     $mainframe =& JFactory::getApplication('site');
     $mainframe->initialise();
     JPluginHelper::importPlugin('system');
     JDEBUG ? $_PROFILER->mark('afterInitialise') : null;
     $mainframe->triggerEvent('onAfterInitialise');
    
     $user =& JFactory::getUser();
    
        if ($user->guest) {
            echo 'stuff';
                //redirect('/');
        } else {
            echo 'user';
        }
    
    0 讨论(0)
  • 2020-12-08 03:51

    The solution showed by Stefan Gehrig

    define( '_JEXEC', 1 );
    define('JPATH_BASE', dirname(dirname(__FILE__)));
    define( 'DS', DIRECTORY_SEPARATOR );
    
    require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
    require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
    
    $mainframe = JFactory::getApplication('site');
    

    works fine, I have spent many long nights trying access the Joomla! resources outside the joomla folder.

    $session     = &JFactory::getSession();
    

    In the follow up code, works fine when the getApplication method has been invoked.

    Thanks for solution.

    0 讨论(0)
  • 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');
    
    0 讨论(0)
  • 2020-12-08 03:51

    I put below code in Joomla index.php and it's work fine for me.

    //Set session to access it outside
    $user =& JFactory::getUser();
    $username = $user->get('username');

    session_start();
    $_SESSION['username'] = $username;

    Now you can use session variable outside Joomla as below

    session_start();
    $_SESSION['username'];

    0 讨论(0)
  • 2020-12-08 03:59

    Actually that's not as easy as it sounds. Joomla uses its own session handling with come unique session-id-generation and some encryption in place, so the only way to get into the Joomla session data is to use the appropriate Joomla functions (as others have suggested). I recently had a project where we needed to transfer a Joomla authenticated user into a separate application. We did this by adding a Joomla adapter which instantiates the Joomla user classes, reads the user data, puts everything into an encrypted cookie and redirects back to our application. In there we read the encrypted cookie, instantiate our own user object and discard the cookie. As this is not 100% secure we're changing the system to write the user data in a database table and read it from our application - we avoid the unsecure way through a cookie that way, because even though the cookie is encrypted (and contains sensitive user information which suffice to authenticate a user) it'll be transfered on wire and could be sniffed.

    define( '_JEXEC', 1 );
    define('JPATH_BASE', dirname(dirname(__FILE__)));
    define( 'DS', DIRECTORY_SEPARATOR );
    
    require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
    require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
    
    $mainframe = JFactory::getApplication('site');
    

    The above is the basic script required to access Joomla resources.

    0 讨论(0)
  • 2020-12-08 04:00

    I assume that by application you mean another website. Your best bet is to have an iframe in that application instantiating the Joomla startup file, get the user id in that iframe, store it somewhere in the database along with your current session id, and then retrieve it by the other application. Will take some time though.

    0 讨论(0)
提交回复
热议问题