簡體   English   中英

如何在joomla 3.2中獲取用戶名

[英]How to get username in joomla 3.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::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

我的編碼有問題嗎? 任何幫助將不勝感激。

嘗試這個,

我認為該框架無法在外部頁面上正確加載。

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

不要為Joomla使用session_start()如果需要使用會話,請嘗試使用Joomla會話庫。

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

您的情況是登錄的管理員名稱。

echo $user->username;

希望它能工作..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM