Close

Accessing Joomla Objects from an External Script

Sometimes one needs to access Joomla’s functions and variables from an external script. Typically one should write a Joomla extension to do so but maybe you want something done quick, or you just need a little integration to another system, e.g. I’ve often had the need to grab the name or id of a user and write a record to another database to link them, find what page a user came from, check their access level, etc, etc,

Obviously just having the code in Joomla’s folder isn’t enough – the application needs to be loaded and looking through the code to find the correct way of doing so isn’t immediately obvious — Joomla contains thousands of files after all. Not to worry, they make it quite easy. Just add the following code to your php script file and get access to Joomla’s goodness.
From there on in you will be able to access various Joomla variables, e.g. use the following to get the user object for the currently logged in user – assuming this script is called in a link from Joomla.

<?php define( '_JEXEC', 1 );
define( 'JPATH_BASE', "/var/www/mywebsite" );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' ); 
$mainframe =& JFactory::getApplication('site'); ?>

Get the user with

$theUser =& JFactory::getUser();

Or use other JFactory functions like getConfig() or getSession()

Should work in Joomla 1.5 upwards. A simple way to add some simple Joomla integration to your scripts.

3 thoughts on “Accessing Joomla Objects from an External Script

  1. Hello,
    > Just add the following code to your php script file and get access to Joomla’s goodness.
    An where ai find this php script file?

    Thanks

    1. Hi Miku, it’s not a joomla script file I am referring to. Rather if you need to access Joomla’s functions and data from your own scripts you can use the instructions above. e.g. say you have a custom script to allow users to sign up to some features on your website and need to perform extra functions on their account once payment is verified. You could create a custom subscribetofeatures.php in your joomla root, process payment from there and still be able to access Joomla’s functions using the code above, otherwise you’re limited to talking to the database directly.

  2. Hi, I looking for how can connect external database, and how can get and public data on artcicel. from extenal DB.

    miku

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.