我正在尝试在Joomla之外(而不是作为插件)运行应用程序,并且我想要访问已登录用户的信息(userid)。 我想知道如何解决这个问题? 是否有一个文件可以包含? 我尝试使用$_SESSION,但它为空。
是否有简单的解决方案? 感谢您的时间。
实际上这并不像听起来那样简单。Joomla使用自己的会话处理,具有独特的会话ID生成和加密功能,因此要想获取Joomla会话数据的唯一方法是使用相应的Joomla函数(正如其他人所建议的那样)。最近我有一个项目,我们需要将一个经过Joomla身份验证的用户转移到另一个应用程序中。我们通过添加一个Joomla适配器来完成此操作,该适配器实例化Joomla用户类,读取用户数据,将所有内容放入加密cookie中,并重定向回我们的应用程序。在那里,我们读取加密cookie,实例化自己的用户对象并丢弃cookie。由于这种方法不是100%安全的,因此我们正在更改系统以将用户数据写入数据库表并从我们的应用程序中读取它-我们通过这种方式避免了通过cookie的不安全方式,因为即使cookie被加密了(并包含足以验证用户的敏感用户信息),它也会在传输中传输,并可能被嗅探。
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(dirname(__FILE__)));
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');
上述是访问 Joomla 资源所需的基本脚本。
define( '_JEXEC', 1 );
define('JPATH_BASE', 'your joomla basedir goes here' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
JDEBUG ? $_PROFILER->mark( 'afterLoad' ) : null;
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
JPluginHelper::importPlugin('system');
JDEBUG ? $_PROFILER->mark('afterInitialise') : null;
$mainframe->triggerEvent('onAfterInitialise');
$user =& JFactory::getUser();
if ($user->guest) {
echo 'stuff';
//redirect('/');
} else {
echo 'user';
}
session_set_cookie_params(86400, '/', '.example.com');
86400是会话的生命周期,将其设置为您喜欢的时间(86400表示一天)。 "/"是cookie的路径。这意味着,如果您的Joomla网站位于http://example.com/joomla/,则仍会发送会话cookie,如果用户访问http://example.com/。
".example.com"是域名。请注意开头的点号,它非常重要。它表示会话cookie将在example.com的任何子域上发送。如果没有它,则cookie仅针对以http://example.com/开头的地址发送。
这应该解决您的问题,除非您正在尝试从另一个域访问会话数据。如果是这种情况,请在此留言,我会看看能否找到解决方法。
由 Stefan Gehrig 展示的解决方案
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(dirname(__FILE__)));
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');
很好用,我花了很多个晚上试图从Joomla!文件夹外部访问资源。
$session = &JFactory::getSession();
getApplication
方法时,可以正常工作。define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE',$_SERVER['DOCUMENT_ROOT'].DS. basename(dirname(__DIR__)) );
其中:JPATH_BASE代表您网站的根目录,必须正确。
之后,您需要按以下方式使用关键文件:
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
jimport( 'joomla.user.user');
jimport( 'joomla.session.session');
jimport( 'joomla.user.authentication');
因此,您文件的核心代码如下:
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');
mod_login.php
在这之后:
$user =& JFactory::getUser();
echo "<p>您的用户类型是{$user->usertype},其组ID为{$user->gid}.</p>";
要获取用户ID,您需要使用Joomla的函数:
$user =& JFactory::getUser();
$user->get('id');
将让您获得用户ID。但是,您需要在Joomla页面内执行此操作,因此我不知道这对您有多大用处。
获取 Joomla 用户 id 的方法:
$user =& JFactory::getUser();
$user_id = $user->get('id');
获取用户会话 ID,请使用以下代码:
$session = & JFactory::getSession();
$session_id = $session->getId();
http://www.php.net/manual/en/function.session-decode.php#79244
我将以下代码放在Joomla的index.php中,对我来说运行得很好。
//设置会话以在外部访问
$user =& JFactory::getUser();
$username = $user->get('username');
session_start();
$_SESSION['username'] = $username;
现在您可以在Joomla之外使用会话变量,如下所示
session_start();
$_SESSION['username'];
JPATH_BASE
常量到 Joomla 安装路径所在的位置,脚本的位置就无关紧要了。 - Stefan Gehrig