简体   繁体   中英

How to get username in joomla 3.2

I had built an external application which retrieve Joomla User Information and I user the code as below :

session_start();
define( '_JEXEC', 1 );
define('JPATH_BASE', '../' );
require_once ( JPATH_BASE .'../includes/defines.php' );
require_once ( JPATH_BASE .'../includes/framework.php' );
$app = JFactory::getApplication('site');
$user = JFactory::getUser();
$myuserid = $user->username;
$_SESSION["myid"] = "$myuserid";
$myid = $_SESSION["myid"];
echo 'User name: ' . $myid . '<br />';

However, the output of $myid is not displayed in the site. It just displayed as :

User name :

The displayed should be :

User name : admin

Is there's a problem with my coding? Any help will be appreciated.

Try this,

I think the framework not loaded correctly on the external page.

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
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();
$user = JFactory::getUser();
echo '<pre/>';
print_r($user);

Do not use session_start() for Joomla If you need to use session try Joomla session library.

$session = JFactory::getSession();
$session->set('variable','your_value');
echo $session->get('variable');

In your case the logged in admin name will be.

echo $user->username;

Hope it works..

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