Including Joomla Topic is solved

For Joomla! 3.x Coding related discussions, please use:

Moderator: ooffick

Forum rules
Please use the mailing list here: rather than this forum.
Post Reply
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Mon Oct 01, 2018 4:06 am

Including Joomla

Post by ZacWarham » Fri Nov 09, 2018 1:34 pm

I am running Joomla 3.9 and have a JS file with an AJAX request to a PHP file I have created in the (custom) /ajax directory

I am trying to get the userID of the logged in user within that PHP file so that I can run some SQL queries based upon that user

I have the following code but it does not work. I think it is an issue importing the files?

Code: Select all

include_once "/includes/defines.php";
	include_once "/includes/framework.php";
	include_once "/libraries/joomla/database/factory.php";
	include_once "/libraries/classmap.php";
	$user =& JFactory::getUser();
	$user_id = $user->id;
Anyone know what the issue is?

User avatar
Joomla! Master
Joomla! Master
Posts: 10955
Joined: Thu Feb 15, 2007 5:48 am
Location: Oxford, UK

Re: Including Joomla

Post by toivo » Fri Nov 09, 2018 3:17 pm

The application needs to be instantiated, too. Use this:

Code: Select all

define('_JEXEC', 1);    
// run from a subfolder e.g. 'scripts' under the main joomla folder
define('JPATH_BASE', realpath(dirname(__FILE__) . '/..'));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';
// instantiate application
$app 	= JFactory::getApplication('site');
$user	= JFactory::getUser();
echo 'user id = ' . $user->id . PHP_EOL;
Toivo Talikka, Global Moderator
troubleshooting smtp and other articles

Post Reply

Return to “Joomla! 3.x Coding”