title: How to get current Joomla user with external PHP script
body: <p>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?</p>
  
  <h3>joomla-auth.php (located in the same directory as Joomla's index.php):</h3>
  
  <pre><code>&lt;?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 =&amp; JFactory::getApplication('site');
  
  /* Make sure we are logged in at all. */
  if (JFactory::getUser()-&gt;id == 0)
      die("Access denied: login required.");
  
  ?&gt;
  </code></pre>
  
  <h3>test.php:</h3>
  
  <pre><code>&lt;?php
  
  include 'joomla-auth.php';
  
  echo 'Logged in as "' . JFactory::getUser()-&gt;username . '"';
  
  /* We then proceed to access things only the user
     of that name has access to. */
  ?&gt;
  </code></pre>