Including Joomla Topic is solved

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
ZacWarham
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
toivo
Joomla! Exemplar
Joomla! Exemplar
Posts: 9940
Joined: Thu Feb 15, 2007 5:48 am
Location: Oxford, UK
Contact:

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

<?php
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
my first programs were assembled and run in 16KB :)
troubleshooting smtp and other articles https://talikka.com/joomla


Post Reply

Return to “Joomla! 3.x Coding”