How to get current Joomla user with external PHP script

Posted by Joey Adams on Stack Overflow See other posts from Stack Overflow or by Joey Adams
Published on 2010-03-15T07:00:35Z Indexed on 2010/03/15 7:09 UTC
Read the original article Hit count: 656

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. */
?>

© Stack Overflow or respective owner

Related posts about joomla

Related posts about php