How to get user session info from external php script with Joomla 4?

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
amer1404
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Mon Dec 07, 2009 6:03 am

How to get user session info from external php script with Joomla 4?

Post by amer1404 » Sat Sep 30, 2023 10:58 am

How we can get the logged-in user information and session information from the external php script with Joomla-4.x?

The below code works with Joomla 3.x but doesn't work with Joomla-4.x

==================================================================
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../..' ));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
$session = JFactory::getSession();
jimport( 'joomla.application.module.helper' );

$session = $session->getId();
$db = JFactory::getDbo();
$user =& JFactory::getUser();
$userid= $user->id;
===================================================================

User avatar
ceford
Joomla! Hero
Joomla! Hero
Posts: 2290
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: How to get user session info from external php script with Joomla 4?

Post by ceford » Sun Oct 01, 2023 2:33 pm

An idea to try:

From the command line, cd to the cli folder of your site and type:

php joomla.php user:list

You get a list of all registered users. This is not what you want but maybe you can create something similar to fetch only logged in users, such as in the Who's online module. Use php joomla.php to get a list of all available commands.

amer1404
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Mon Dec 07, 2009 6:03 am

Re: How to get user session info from external php script with Joomla 4?

Post by amer1404 » Sun Oct 01, 2023 5:34 pm

Thanks, Dear @ceford

Actually, I need to get the user session from the PHP script not command line in order to handle data from/to the database using the JFactory package

e.g :

Code: Select all


<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../..' ));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
$session = JFactory::getSession();
jimport( 'joomla.application.module.helper' );

$session_id = $session->getId();
$db = JFactory::getDbo();
$user =& JFactory::getUser();
$user_id= $user->id;

if ($session_id != NULL && $user_id != NULL){


$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('student_id', 'student_name')));
$query->from($db->quoteName('#__student'));
$db->setQuery($query);

$db->execute();
$total_records = $db->getNumRows();
$result = $db->loadRowList();
//......
//..........
//..............etc

}
else { echo"invalid request";}
?>

robbiej
Joomla! Apprentice
Joomla! Apprentice
Posts: 29
Joined: Wed Apr 06, 2016 11:22 am

Re: How to get user session info from external php script with Joomla 4?

Post by robbiej » Fri Oct 13, 2023 5:01 pm

I'm assuming you're running the server script from a client browser, and assuming that the user has previously logged onto the site. If so, this code worked for me:

Code: Select all

<?php

use Joomla\CMS\Factory;
use Joomla\CMS\Session\Session;

error_reporting(E_ALL);
ini_set('display_errors', 1);

define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__).'/..' ));

require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

// 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
 *
 * In addition to aliasing "common" service keys, we also create aliases for the PHP classes to ensure autowiring objects
 * is supported.  This includes aliases for aliased class names, and the keys for aliased class names should be considered
 * deprecated to be removed when the class name alias is removed as well.
 */
$container->alias('session.web', 'session.web.site')
    ->alias('session', 'session.web.site')
    ->alias('JSession', 'session.web.site')
    ->alias(\Joomla\CMS\Session\Session::class, 'session.web.site')
    ->alias(\Joomla\Session\Session::class, 'session.web.site')
    ->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');

// Instantiate the application.
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);

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

$user = \Joomla\CMS\Factory::getApplication()->getSession()->get('user');

if ($user && $user->id > 0){
    // do query

} else {
    echo "not logged on\n";
}
Some debate about it all at https://groups.google.com/g/joomla-dev- ... 5J2s9hhMxA

amer1404
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Mon Dec 07, 2009 6:03 am

Re: How to get user session info from external php script with Joomla 4?

Post by amer1404 » Sun Oct 15, 2023 5:02 am

Thank dear robbiej for your help!
I really appreciate your input. The code you provided makes sense, and I'll give it a try. It's great to have your guidance as I work on this. Thanks again!


Post Reply

Return to “Joomla! 4.x Coding”