简体   繁体   中英

How can I access login user data using external files in joomla 2.5?

I am having trouble in accessing the data of the login user in joomla site. Can someone help me on it?

I've tried this code:

define( '_JEXEC', 1 );
define( 'DS', '/' );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
require_once( JPATH_BASE . DS .'xampp' . DS .'UPOnlineLeave '. DS .'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS .'xampp' . DS .'UPOnlineLeave '. DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS .'xampp' . DS .'UPOnlineLeave '. DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
    $user =& JFactory::getUser();
    $session =& JFactory::getSession();
$user->username;

but it produces JOSerror:application instantation error .

Can someone help me?

Fisrt you need to load the entire Joomla Framework:

// Get Joomla! framework
define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

Only then you can try to get the user

$user =& JFactory::getUser();
$session =& JFactory::getSession();

You have to include the following lines to include the joomla default libraries.

You can also refer the code on the root path of the project index.php file

define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

if (file_exists(dirname(__FILE__) . '/defines.php')) {
    include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
    define('JPATH_BASE', dirname(__FILE__));
    require_once JPATH_BASE.'/includes/defines.php';
}

require_once JPATH_BASE.'/includes/framework.php';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM