Php 在Joomla之外访问会话数据
我试图在Joomla之外运行一个应用程序(不是作为插件),我想访问登录用户的信息(userid)。我想知道我该怎么做?有我可以包含的文件吗?我尝试使用$\u会话,但它显示为空Php 在Joomla之外访问会话数据,php,joomla,Php,Joomla,我试图在Joomla之外运行一个应用程序(不是作为插件),我想访问登录用户的信息(userid)。我想知道我该怎么做?有我可以包含的文件吗?我尝试使用$\u会话,但它显示为空 我的问题有简单的解决办法吗?感谢您抽出时间。解决方案是为整个域和/或站点设置会话。如果您试图访问joomla作用域之外的会话数据,则此选项适用。例如,如果您的joomla站点位于,而您的其他站点位于,则保存会话id的cookie不会从joomla传输到其他站点。要修改此行为,请在每次会话开始()之前使用会话设置cookie
我的问题有简单的解决办法吗?感谢您抽出时间。解决方案是为整个域和/或站点设置会话。如果您试图访问joomla作用域之外的会话数据,则此选项适用。例如,如果您的joomla站点位于,而您的其他站点位于,则保存会话id的cookie不会从joomla传输到其他站点。要修改此行为,请在每次会话开始()之前使用会话设置cookie参数(我不太了解joomla,但您应该只添加几行代码)。这样使用它:
session_set_cookie_params(86400, '/', '.example.com');
86400是会话的生命周期,请将其设置为您喜欢的值(86400是一天)。
“/”是cookie的路径。这意味着,如果您的joomla站点位于上,那么如果用户访问,会话cookie仍将被发送
“.example.com”是域。注意开头的圆点,这很重要。它表示会话cookie将在example.com的任何子域上发送。如果不将其放入,cookie将仅发送以开头的地址
这应该可以解决您的问题,除非您试图从另一个域访问会话数据。如果是这样的话,请在这里留下评论,我会看看我是否能找到什么。很可能像Wordpress一样,Joomla不使用任何“会话”数据,而是直接从数据库中提取数据。在这种情况下,您需要使用Joomla的本机函数 但这只是我使用Wordpress的经验 已更新 我想我错了。
假设这是用于访问Joomla会话变量的api类: //返回对全局变量的引用 JSession对象,仅在以下情况下创建它 它还不存在$session= &JFactory::getSession() //从会话变量获取值 $value=$session->get('var\u name', 无效) //将值放入会话变量中 $session->set('var_name',$value)
要获取用户id,您需要使用Joomlas函数:
$user=&JFactory::getUser()代码>
$user->get('id')代码>
将让您获得用户ID。但是,您需要在joomla页面中执行此操作,因此我不知道这对您有多大用处。实际上,这并不像听起来那么容易。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资源所需的基本脚本。了解如何在jFusion等应用程序桥中实现这一点可能会有所帮助。我建议至少为Joomla提供一个系统插件,该插件将使用Joomla函数来获取Joomla安装所需的一切,并在application Initialize上交付给您的应用程序。最重要的问题将是ur数据流建模 要获取Joomla用户id,请使用:
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';
}
$user =& JFactory::getUser();
$user_id = $user->get('id');
要获取用户会话id,请使用:
$session = & JFactory::getSession();
$session_id = $session->getId();
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!joomla文件夹之外的资源
$session = &JFactory::getSession();
在后续代码中,当调用getApplication
方法时,该方法可以正常工作
感谢您的解决方案。如果您将会话存储在数据库中,您可以按照以下注释对会话数据进行解码:
在mod\u login.php
之后:
$user=&JFactory::getUser()代码>
echo“您的用户类型是{$user->usertype},其组id为{$user->gid}.”代码>我假设应用程序指的是另一个网站。最好是在该应用程序中有一个iframe实例化Joomla启动文件,在该iframe中获取用户id,将其与当前会话id一起存储在数据库中的某个位置,然后由另一个应用程序检索它。不过需要一些时间。我将下面的代码放在Joomla index.php中,它对我来说很好
//将会话设置为在外部访问它
$user=&JFactory::getUser()
$username=$user->get('username')代码>
session_start()
$\会话['username']=$username代码>
现在您可以在Joomla之外使用会话变量,如下所示
session_start()
$\会话['username']代码>首先,您必须提供一些joomla常量(标识符)的定义,如下所示:
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' );
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');
其中:JPATH_BASE表示站点的根目录。它必须是正确的
之后,您必须按如下方式使用密钥文件:
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' );
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');
之后,您必须创建应用程序对象并对其进行初始化:
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
[这是可选的]如果要导入其他一些库,则可以按如下操作:
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' );
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');
因此,文件的核心代码如下所示:
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' );
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');
我无法告诉您1.5以上版本的Joomla是如何做到这一点的