【问题标题】:How to get username in joomla 3.2如何在 joomla 3.2 中获取用户名
【发布时间】:2014-07-09 03:38:44
【问题描述】:

我建立了一个外部应用程序来检索 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 />';

但是,$myid 的输出不会显示在站点中。它只是显示为:

User name :

显示的应该是:

User name : admin

我的编码有问题吗? 任何帮助将不胜感激。

【问题讨论】:

  • 如果这是外部代码,您在哪里登录 Joomla!系统?
  • 亲爱的 Mike,这是一个名为 mysession.php 的外部系统,将包含在另一个名为 index.php 的页面中
  • 这个问题似乎是题外话,因为它属于Joomla

标签: php session joomla joomla3.0 joomla3.2


【解决方案1】:

试试这个,

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

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);

不要将session_start() 用于Joomla 如果您需要使用会话,请尝试使用Joomla 会话库。

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

在您的情况下,登录的管理员名称将是。

echo $user->username;

希望它有效..

【讨论】:

  • 亲爱的 Jobin,谢谢,但很抱歉,它不起作用。 :( 让我详细解释一下。上面的扩展文件名为 mysession.php 并将在 joomla 中名为 index.php 的包装文件中调用 在 index.php 页面中,我将 '; ?> 如果我以管理员身份登录,那么index.php 页面的输出应该是 User name : admin
  • @KokoroSan 您在 Joomla 框架内使用的这个外部文件有两件事,例如将此文件包含在 Joomla 模板 index.php 或 Joomla 安装文件的一部分中,然后无需将框架加载到页。还有一点就是这个文件的路径不是问题,主要要考虑的是Joomla的安装路径define('JPATH_BASE', dirname(__FILE__) )这里设置Joomla的安装路径。
  • 亲爱的 Jobin,感谢您的解释。我会修复一些编码,这是我运行 mysession.php 文件本身时的输出。 JUser 对象 ( [isRoot:protected] => [id] => 0 [name] => [username] => [email] => [password] => [password_clear] => [block] => [sendEmail] = > 0 [registerDate] => [lastvisitDate] => [activation] => [params] => [groups] => Array ( [0] => 9 ) 我感到困惑
  • 亲爱的 Jobin,哦,伙计...我忘了登录... ^___^ 难怪...好的下一步..我如何调用用户名并将其显示在我的索引中。 php 作为用户名:admin JUser Object ( [isRoot:protected] => [id] => 709 [name] => administrator [username] => admin [email] => admin@yahoo.com
  • 亲爱的乔宾,非常感谢你..它的工作!我欠你的.. ^__^ 再次抱歉打扰你.. ^___^
猜你喜欢
  • 2012-05-15
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多