如何让两个应用 Yii1 和 Yii2 使用同一个会话? [英] How to make two apps Yii1 and Yii2 use the same session?

查看:24
本文介绍了如何让两个应用 Yii1 和 Yii2 使用同一个会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我现有的网站中制作一个仪表板面板网络.现有的应用程序正在使用 Yii1,可以通过例如 www.example.com 访问.我想使用 Yii2 使用新应用程序制作 www.example.com/dashboard.我已经做了apache配置.

I want to make a dashboard panel web in my existing website. The existing app is using Yii1 can be accessible via, let's say, www.example.com. I want to make www.example.com/dashboard using new app using Yii2. I already made the apache configuration.

但是缺少的是会话.通过 Yii1 应用登录的用户将无法在仪表板中识别.

But what's missing is the session. Users that logged in through Yii1 app will not be recognized in the dashboard.

如何让 Yii1 和 Yii2 使用相同的会话数据,即登录到 Yii1 的用户将被 Yii2 识别,反之亦然?

How to make Yii1 and Yii2 use the same session data, i.e. user that logged in at Yii1 will be recognized in Yii2 vice versa?

更新

该架构是负载均衡器后面的标准多网络服务器.这两个应用程序可以托管在同一个服务器实例中(配置将在 apache 中).或者它们可以不同(负载均衡器也会处理目录).但是两者都将使用相同的 memcache 服务器进行会话存储.应用程序也将使用相同的数据库,尽管 ActiveRecord 实现显然将使用不同的代码.只要能拿到当前登录用户的用户ID就可以了.

The architecture is standard multiple web servers behind a load balancer. The two app can be hosted in the same server instance (the config will be in the apache). Or they can be in different (the load balancer will handle the directory too). But both will use the same memcache server for session storage. The apps also will use the same database, although the ActiveRecord implementation will obviously be in different code. As long as I can get the user ID of the current logged user, it should be okay.

推荐答案

可以在两个应用程序(Yii1 和 Yii2)之间共享会话.如果您在 Yii1 中使用 CDbHttpSession 并在 Yii2 中使用 yii\web\DbSession(我没有用其他类型的会话存储对其进行测试,但是它应该工作相同):

It is possible to share session between two applications (Yii1 and Yii2). Here is how to do it if you are using CDbHttpSession in Yii1 and yii\web\DbSession in Yii2 (I didn't test it with other types of session storage, but it should work same):

  • yii\web\DbSession 的 Yii2 配置中,你应该将会话名称设置为 PHPSESSID 以便它可以匹配 Yii1 使用的会话名称.
  • 由于在 Yii1 CWebUser 中使用前缀来存储登录的用户 ID 和来自用户状态的其他内容,您应该将其设置为空字符串(或者您可以在两个应用程序之间共享的任何内容.Yii2 不使用前缀,因此空字符串也很好用)''.您可以通过扩展 CWebUser 并在 public function init() 中添加类似的内容来实现:
  • In Yii2 configuration for yii\web\DbSession for you application you should setup session name to be PHPSESSID so it can match one used by Yii1.
  • Since in Yii1 CWebUser use prefix for storing logged user id and other things from user state, you should set this to be empty string (or what ever you can share between both application. Yii2 doesn't use prefix, so empty string works good too) ''. You can do it by extending CWebUser and add something like this inside public function init():

公共函数init(){$this->setStateKeyPrefix('');父::init();}

这就是在两个应用程序之间共享会话所需的全部内容.

This is all what you need to be able to share session between two application.

这篇关于如何让两个应用 Yii1 和 Yii2 使用同一个会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆