How to get current Joomla user with external PHP script
- by Joey Adams
I have a couple PHP scripts used for AJAX queries, but I want them to be able to operate under the umbrella of Joomla's authentication system. Is the following safe? Are there any unnecessary lines?
joomla-auth.php (located in the same directory as Joomla's index.php):
<?php
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
/* Create the Application */
$mainframe =& JFactory::getApplication('site');
/* Make sure we are logged in at all. */
if (JFactory::getUser()->id == 0)
die("Access denied: login required.");
?>
test.php:
<?php
include 'joomla-auth.php';
echo 'Logged in as "' . JFactory::getUser()->username . '"';
/* We then proceed to access things only the user
of that name has access to. */
?>