How to make two apps Yii1 and Yii2 use the same se

2019-07-22 12:11发布

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.

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

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?

UPDATE

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.

标签: yii yii2
2条回答
唯我独甜
2楼-- · 2019-07-22 12:30

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):

  • 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():

public function init() { $this->setStateKeyPrefix(''); parent::init(); }

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

查看更多
该账号已被封号
3楼-- · 2019-07-22 12:37

I am not sure it is possible, I know yii2 has a setting for the cookie where the auth details is stored. You might be able to do the same thing in yii1

Take a look here https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-shared-hosting.md#separate-sessions-and-cookies you want to do the oposite, share the cookie between them.

Btw I do not believe it is possible if the 2 apps are on different domains.

Another solution I can think of is to try to move the session to something like memcached and use the same key for both apps.

Also take a look here: http://www.yiiframework.com/doc-2.0/guide-tutorial-yii-integration.html you might find something interesting. I believe you may be able to log the user in both places at once, that also might solve your problem.

查看更多
登录 后发表回答