How to get username in joomla 3.2

北城以北 提交于 2019-12-13 06:59:22

问题


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.


回答1:


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..



来源:https://stackoverflow.com/questions/24645008/how-to-get-username-in-joomla-3-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!