php 在 Joomla 之外访问会话数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1175598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:21:10  来源:igfitidea点击:

Accessing session data outside Joomla

phpjoomla

提问by Alec Smart

I am trying to run an application outside Joomla (not as a plugin) and I would like to access the logged in user's information (userid). I am wondering how should I go about doing that? Is there a file which I can include? I tried using $_SESSION but it shows empty.

我试图在 Joomla 之外运行一个应用程序(不是作为插件),我想访问登录用户的信息(用户 ID)。我想知道我该怎么做?有我可以包含的文件吗?我尝试使用 $_SESSION 但它显示为空。

Is there a simple solution to my problem? Thank you for your time.

我的问题有简单的解决方案吗?感谢您的时间。

回答by Stefan Gehrig

Actually that's not as easy as it sounds. Joomla uses its own session handling with come unique session-id-generation and some encryption in place, so the only way to get into the Joomla session data is to use the appropriate Joomla functions (as others have suggested). I recently had a project where we needed to transfer a Joomla authenticated user into a separate application. We did this by adding a Joomla adapter which instantiates the Joomla user classes, reads the user data, puts everything into an encrypted cookie and redirects back to our application. In there we read the encrypted cookie, instantiate our own user object and discard the cookie. As this is not 100% secure we're changing the system to write the user data in a database table and read it from our application - we avoid the unsecure way through a cookie that way, because even though the cookie is encrypted (and contains sensitive user information which suffice to authenticate a user) it'll be transfered on wire and could be sniffed.

事实上,这并不像听起来那么容易。Joomla 使用它自己的会话处理,带有独特的会话 ID 生成和一些适当的加密,因此进入 Joomla 会话数据的唯一方法是使用适当的 Joomla 功能(正如其他人所建议的那样)。我最近有一个项目,我们需要将经过 Joomla 身份验证的用户转移到一个单独的应用程序中。我们通过添加一个 Joomla 适配器来实现这一点,该适配器实例化 Joomla 用户类、读取用户数据、将所有内容放入加密的 cookie 中并重定向回我们的应用程序。在那里我们读取加密的 cookie,实例化我们自己的用户对象并丢弃 cookie。由于这不是 100% 安全,我们正在更改系统以将用户数据写入数据库表并从我们的应用程序中读取它 - 我们通过这种方式避免了通过 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');

The above is the basic script required to access Joomla resources.

以上是访问Joomla资源所需的基本脚本。

回答by jkatzer

 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';
    }

回答by FWH

The solution is to set the session for your whole domain and/or site. It applies if you're trying to access the session data outside of joomla scope. For example, if your joomla site is located on http://example.com/joomla/and your other site on http://othersite.example.com/then the cookie holding the session id is not transmitted from joomla to the other site. To modify this behaviour, use session_ set_ cookie_ params before every session_start()(I don't know joomla very well, but you should have to add only a few lines of code). Use it this way:

解决方案是为整个域和/或站点设置会话。如果您尝试访问 joomla 范围之外的会话数据,则它适用。例如,如果您的 joomla 站点位于http://example.com/joomla/而您的其他站点位于http://othersite.example.com/则保存会话 ID 的 cookie 不会从 joomla 传输到另一个站点地点。要修改此行为,请在每个 session_start() 之前使用 session_ set_ cookie_ params (我不太了解 joomla,但您应该只需要添加几行代码)。以这种方式使用它:

session_set_cookie_params(86400, '/', '.example.com');

86400 is the lifetime of the session, set it to what you prefer (86400 is one day). '/' is the path of the cookie. It means that if your joomla site is located on http://example.com/joomla/, the session cookie will still be sent if the user accesses http://example.com/.

86400 是会话的生命周期,将其设置为您喜欢的(86400 是一天)。'/' 是 cookie 的路径。这意味着如果您的 joomla 站点位于http://example.com/joomla/,则如果用户访问http://example.com/,会话 cookie 仍将被发送。

'.example.com' is the domain. Note the dot at the beginning, it's very important. It says that the session cookie will be sent on any subdomain of example.com. If you don't put it, the cookie will be sent only for addresses starting with http://example.com/.

'.example.com' 是域。注意开头的点,这很重要。它表示会话 cookie 将在 example.com 的任何子域上发送。如果您不输入,cookie 将仅针对以http://example.com/开头的地址发送。

This should solve your problem, unless you are trying to access the session data from another domain. If it's the case, leave a comment here, I'll see if I cand find something.

这应该可以解决您的问题,除非您尝试从另一个域访问会话数据。如果是这样,请在此处发表评论,我会看看是否可以找到一些东西。

回答by Carlos Spohr

The solution showed by Stefan Gehrig

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

works fine, I have spent many long nights trying access the Joomla! resources outside the joomla folder.

工作正常,我花了很多个漫长的夜晚试图访问 Joomla!joomla 文件夹外的资源。

$session     = &JFactory::getSession();

In the follow up code, works fine when the getApplicationmethod has been invoked.

在后续代码中,当getApplication方法被调用时工作正常。

Thanks for solution.

感谢您的解决方案。

回答by Carlos Spohr

First of all you have to provide definition to some joomla's constants(identifiers) as follows:

首先,您必须为某些 joomla 的常量(标识符)提供定义,如下所示:

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE',$_SERVER['DOCUMENT_ROOT'].DS. basename(dirname(__DIR__)) );

where: JPATH_BASE is represents your site's root directory. It must be correct.

其中: JPATH_BASE 代表您站点的根目录。它必须是正确的。

After than, you have to use key files as follows:

之后,您必须按如下方式使用密钥文件:

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

After than, you have to create an application object and initialize it also:

之后,您必须创建一个应用程序对象并对其进行初始化:

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();  

[this is optional] If you want to import some other libraries, then you can do this as follows:

[这是可选的] 如果你想导入一些其他的库,那么你可以这样做:

jimport( 'joomla.user.user');
jimport( 'joomla.session.session');
jimport( 'joomla.user.authentication');

So the core code for your file is as follows:

所以你的文件的核心代码如下:

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

回答by rishabh

apply this in mod_login.php

将此应用在 mod_login.php

After: $user =& JFactory::getUser();

后: $user =& JFactory::getUser();

echo "<p>Your usertype is {$user->usertype} which has a group id of {$user->gid}.</p>";

echo "<p>Your usertype is {$user->usertype} which has a group id of {$user->gid}.</p>";

回答by schubySteve

to get the user id you need to use Joomlas functions:

要获取您需要使用 Joomlas 功能的用户 ID:

$user =& JFactory::getUser();
$user->get('id');

$user =& JFactory::getUser();
$user->get('id');

will let you get the user ID. you will however need to do this inside of the joomla page so i dont know how usefult hat will be to you.

会让你得到用户ID。但是,您需要在 joomla 页面内执行此操作,因此我不知道这对您有多大用处。

回答by itoctopus

I assume that by application you mean another website. Your best bet is to have an iframe in that application instantiating the Joomla startup file, get the user id in that iframe, store it somewhere in the database along with your current session id, and then retrieve it by the other application. Will take some time though.

我假设您所说的应用程序是指另一个网站。最好的办法是在该应用程序中使用 iframe 实例化 Joomla 启动文件,在该 iframe 中获取用户 ID,将其与当前会话 ID 一起存储在数据库中的某个位置,然后由其他应用程序检索它。不过需要一些时间。

回答by Hitesh Patel

I put below code in Joomla index.php and it's work fine for me.

我把下面的代码放在 Joomla index.php 中,它对我来说很好用。

//Set session to access it outside
$user =& JFactory::getUser();
$username = $user->get('username');

//Set session to access it outside
$user =& JFactory::getUser();
$username = $user->get('username');

session_start();
$_SESSION['username'] = $username;

session_start();
$_SESSION['username'] = $username;

Now you can use session variable outside Joomla as below

现在您可以在 Joomla 之外使用会话变量,如下所示

session_start();
$_SESSION['username'];

session_start();
$_SESSION['username'];

回答by user3126146

I cannot tell you how Joomlawith versions above 1.5 does that but in Joomla 1.5here is how you do that: ( I am sure for other versions procedure is very similar )

我不能告诉你如何的Joomla与上述版本1.5这是否但是在的Joomla 1.5这里是你如何做到这一点:(我相信对于其他版本的程序非常相似)

Joomla generates Unique session id for front-end of the website and back-end. To access session data all you need is know the session id.

Joomla 为网站的前端和后端生成唯一的会话 ID。要访问会话数据,您只需要知道会话 ID。

In joomla configuration file there is a parameter called "secret"

在 joomla 配置文件中有一个名为“ secret”的参数

For back-end this is how you generate session id:

对于后端,这是生成会话 ID 的方式:

$session_id = md5( md5( JConfig::$secret.'administrator' ) );

and for front end:

对于前端:

$session_id = md5( md5( JConfig::$secret.'site' ) );

After this a simple query

在此之后一个简单的查询

mysql_query( 'SELECT `data` FROM jos_session WHERE session_id="'.$sessionId.'"  )

will give you access to session data. All you need is to decrypt it with session_decodeand session data will be in $_SESSIONvariable.

将使您能够访问会话数据。您只需要使用session_decode解密它, 会话数据将在$_SESSION变量中。

Don't forget to put session_startbefore session_decodeotherwise it will not work

不要忘了把在session_startsession_decode否则将无法正常工作