0

I'm updating a working J3 system where one menu item is an iframe wrapper which runs an external php program which needs to know the current user. I've been updating the first part of this file to use the new J4 architecture. I've based it on app.php and the file starts like this...

define( '_JEXEC', 1 );
    
if (file_exists(dirname(__DIR__) . '/defines.php')) {
    include_once dirname(__DIR__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__DIR__));
    require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';

    $class_name             =  new \Joomla\CMS\Application\AdministratorApplication;
    $session_alias          = 'session.web';
    $session_suffix         = 'web.administrator';
    
// Boot the DI container
$container = \Joomla\CMS\Factory::getContainer();


//Alias the session service keys to the web session service as that is the primary session backend for this application
 $container->alias($session_alias, 'session.' . $session_suffix)
                ->alias('JSession', 'session.' . $session_suffix)
                ->alias(\Joomla\CMS\Session\Session::class, 'session.' . $session_suffix)
                ->alias(\Joomla\Session\Session::class, 'session.' . $session_suffix)
                ->alias(\Joomla\Session\SessionInterface::class, 'session.' . $session_suffix);


// Instantiate the application.
$app = $container->get($class_name::class);

// Set the application as global app
\Joomla\CMS\Factory::$application = $app;

use Joomla\CMS\Factory;
$user = Factory::getUser();

The program then continues and does something useful!

However I'm missing something obvious as at the end of this code, $user->guest is 1 and $user->name, etc are all blank.

This implies that no one is logged in but I AM logged in the J! instance passing control to this file and this is confirmed by looking at the 'Session' section of the J! debug bar. What am I missing to make this work?

3
  • Is the menu item in the front end or the back end of Joomla? The code you are using is running in the back end so if you are not logged into the administrator interface then it wont find an active session. Is the URL you are calling the same domain as the Joomla instance you are logged in to? And the J4 method to get the current user is Factory::getApplication()->getIdentity()
    – Irata
    Aug 8, 2023 at 8:42
  • And how did the current J3 code pass the userid across?
    – Irata
    Aug 8, 2023 at 8:47
  • Menu item is front end. Yes - url is same domain and a subfolder under the J! root. Working J3 code is... define( '_JEXEC', 1 ); define( 'JPATH_BASE', str_replace('SG-apps','',dirname(FILE)) ); //define( 'JPATH_BASE', '/home/phouse/public_html' ); define( 'DS', '/' ); require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' ); require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' ); JFactory::getApplication('site')->initialise(); $user = JFactory::getUser(); $joomla_name = $user->name; $joomla_email = $user->email;
    – SteveG
    Aug 8, 2023 at 11:18

1 Answer 1

0

You need to call the Site, front-end, instance of the Joomla application and then it will use the front end session details of the logged in user.

Replace these lines in your code.

 $class_name = new \Joomla\CMS\Application\SiteApplication;
 $session_alias = 'session.web';
 $session_suffix = 'web.site';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.