define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );
$user = JFactory::getUser();
echo 'User name: ' . $user->username . '<br />';
echo 'Real name: ' . $user->name . '<br />';
$specificuser = JFactory::getUser(42);
echo 'Specific User name: ' . $specificuser->username . '<br />';
echo 'Specific Real name: ' . $specificuser->name . '<br />';
我在我的外部php文件中添加了这段代码。$specificuser
代码正常工作;但有时显示$user
输出,有时不显示。知道是什么问题吗?输出如下:
User name:
Real name:
Specific User name: user
Specific Real name: Super User
JFactory::getUser()
方法加载当前用户对象:
- 当您登录时:它将返回您的数据。
- 当您注销时:您是一个没有名字的访客/匿名访客。
第二种情况也适用于当你登录,但你的会话过期
你没有启动Joomla!应用程序,所以会话处理和其他事情不工作。
关于如何初始化Joomla的工作示例,请参阅我对类似问题的回答!正确。