[英]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應用程序,因此您沒有任何“當前”用戶。 您必須通過代碼執行登錄,這可以完成,但是不幸的是需要純文本用戶名和密碼。
$login_result = $mainframe->login(array(
'username' => $username,
'password' => $password
));
if (!$login_result)
{
throw new \Exception("Unable to set current user!", 401);
}
之后,新的Joomla應用程序將擁有一個當前用戶,您可以執行通常的JFactory::getUser()
如果沒有用戶名和密碼(如我所願),則有兩種選擇:
謝謝大家幫助。 Elin提出的解決方案是將用戶名放在url中。 為此,它涉及Joomla核心的破解,因為模板替代似乎不適用於此文件。
查找這個文件
組件\\ com_wrapper \\視圖\\ 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'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.