Php 如何在joomla 3.2中获取用户名

Php 如何在joomla 3.2中获取用户名,php,session,joomla,joomla3.0,joomla3.2,Php,Session,Joomla,Joomla3.0,Joomla3.2,我已经构建了一个检索Joomla用户信息的外部应用程序,我使用的代码如下: session_start(); define( '_JEXEC', 1 ); define('JPATH_BASE', '../' ); require_once ( JPATH_BASE .'../includes/defines.php' ); require_once ( JPATH_BASE .'../includes/framework.php' ); $app = JFactory::getApplicat

我已经构建了一个检索Joomla用户信息的外部应用程序,我使用的代码如下:

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 />';
显示的内容应为:

User name : admin
我的编码有问题吗? 任何帮助都将不胜感激。

试试这个

我认为外部页面上没有正确加载框架

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);
在您的情况下,登录的管理员名称将为

echo $user->username;

希望它能工作。

如果这是外部代码,那么您在哪里登录Joomla!系统?亲爱的Mike,这是一个名为mysession.php的外部系统,将包含在另一个名为index.php的页面中。这个问题似乎离题了,因为它属于亲爱的Jobin,谢谢,但很抱歉它不起作用(让我详细解释一下。上面名为mysession.php的扩展文件将在index.php页面的joomla包装文件index.php中调用,我认为如果我以管理员身份登录,index.php页面的输出应该是用户名:admin@KokoroSan这个外部文件有两件事你正在使用JooMLA框架内的工作,比如JoMLA模板索引文件.php或JoOMLA安装文件的一部分,然后不需要将框架加载到页面上。另一个问题是这个文件的路径不是问题,你必须考虑的是JoOMLA安装的路径<代码>定义('jPaseBase',DrimNeX)。(_FILE__))这里设置Joomla安装的路径。亲爱的Jobin,感谢您的解释。我会修复一些编码,这是运行mysession.php文件本身时的输出。JUser对象([isRoot:protected]=>[id]=>0[name]=>[username]=>[email]=>[password]=>[password\u clear]=>[block]=>[sendEmail]=>0[registerDate]=>[lastvisitDate]=>[activation]=>[params]=>[groups]=>数组([0]=>9)我感到困惑亲爱的Jobin,哦,伙计…我忘了登录…^难怪…下一步…我如何调用用户名并在index.php中将其显示为用户名:admin JUser Object([isRoot:protected]=>[id]=>709[name]=>administrator[username]=>admin[email] => admin@yahoo.comDear乔宾,非常感谢你。这是工作!!!我欠你的。^ ^ ^再次抱歉打扰你这么多^___^
$session = JFactory::getSession();
$session->set('variable','your_value');
echo $session->get('variable');
echo $user->username;