joomla3访问joomla外部的当前用户对象


joomla3 access current user object outside joomla

我想将登录的用户名插入一个变量中,以便在Joomla灌输的根目录中的页面中使用,该页面通过包装器/iFrame显示。

在主页中设置cookie不起作用,访问$user=&JFactory::getUser((;在下面的代码后面(下面的链接(。有什么想法吗?

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE',$_SERVER['DOCUMENT_ROOT'].DS. basename(dirname(__DIR__)) );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
//optional use depend on requirement 
jimport( 'joomla.user.user');
jimport( 'joomla.session.session');
jimport( 'joomla.user.authentication');

访问Joomla 之外的会话数据

使用这些代码,您将创建并初始化一个新的Joomla应用程序,因此您没有任何"当前"用户。您必须通过代码执行登录,这是可以完成的,但不幸的是,需要纯文本用户和密码。

$login_result = $mainframe->login(array(
    'username' => $username,
    'password' => $password
));
if (!$login_result)
{
    throw new 'Exception("Unable to set current user!", 401);
}

之后,新的Joomla应用程序将有一个当前用户,您可以执行通常的JFactory::getUser()

如果你没有用户名和密码(正如我所希望的(,你有两个选择:

  • 使用Plug-Master用户插件,这样您就可以使用管理员密码登录任何用户
  • 创建一个自定义身份验证插件,在确保登录请求来自外部页面后,该插件允许在不检查密码的情况下进行身份验证

感谢大家的帮助。Elin提出的解决方案是将用户名放在url中。要做到这一点,需要对Joomla核心进行黑客攻击,因为模板覆盖似乎不适用于此文件。

查找此文件

components''com_wrapper''views''wrapper''tmpl''default.php

//Insert line below line after this
defined('_JEXEC') or die;
$user = JFactory::getUser();

44号线附近

//change 
src="<?php echo $this->escape($this->wrapper->url);
//to this
src="<?php echo $this->escape($this->wrapper->url). "?userId=" . $user->id; ?>"

//then in the file in the iframe
$userid2 = $_GET["userId"];
//or if you want to store the user name in a session variable
$userid3 = $_SESSION['userId'];