I have an application in my Joomla site, that gets userId. In my local machine I don't have problem, but in my server hosting (Godaddy), doesn't work.

define( '_JEXEC', 1 ); define('JPATH_BASE', realpath(dirname(__FILE__).'/../..' )); require_once ( JPATH_BASE .'/includes/defines.php' ); require_once ( JPATH_BASE .'/includes/framework.php' ); $mainframe =& JFactory::getApplication('site'); $mainframe->initialise(); $user = JFactory::getUser(); $userId = $user->id;

I would also like to install PDO in server hosting

  • Have you bade sure JPATH_BASE is defined correctly? Why not simply use var_dump(JPATH_BASE); to see if it shows you the root of your Joomla directory. Also, you can remove the & before JFactory
    – Lodder
    Nov 14, 2015 at 14:44

2 Answers 2


I use:


it works both on my local windows machine and my sites on up on siteground. Sorry, can't remember where I found it.


    // defining the base path.
    require_once (JPATH_BASE .'/includes/defines.php' );
    require_once( JPATH_BASE .'/includes/framework.php' );

    // Creating an app instance 
    $app =& JFactory::getApplication('site');

    jimport( 'joomla.user.user' );
    jimport( 'joomla.user.helper' );
    $user = JFactory::getUser();


$createdby = $user->id;

Take a copy of /index.php and call it /user_id.php

remove the last line:

// Execute the application.

and put in this line:

echo JFactory::getUser()->id;

Note that you need to be logged in to Joomla and have an active session for this to echo out your ID.

As for the JPATH_BASE, well you need to ensure that the real path you are creating actually is correct, so echo it out with

echo realpath(dirname(__FILE__) . '/../..' );

and ensure thats a valid path. In later (I mean secure) versions of PHP you can use the PHP __DIR__ constant for the folder you are in e.g.

echo realpath(__DIR__ . '/../..' );

This directory name does not have a trailing slash unless it is the root directory - see: http://php.net/manual/en/language.constants.predefined.php

First get this working in the root folder of Joomla, next to the index.php and then you can move it up your two folder levels if thats where you need it.

You might also want to add some security somewhere....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.