没有不值得去解决的问题,也没有不值得去学习的技术!

Sync Login State With Sessions in ThinkPHP 3.2.3beta: Send Login and Logout Notifications via UCenter

Shared print result of $_SESSION in .xxx.com

Because passport.xxx.com and soft.xxx.com are two second-level subdomains of xxx.com, when both are in a logged-out state, a synchronous login notification is sent via UCenter to implement login. However, in many cases, passport.xxx.com and soft.xxx.com might have one in a logged-in state and the other in a logged-out state. At this point, it is necessary to implement login state synchronization; otherwise, clicking the login link on soft.xxx.com to go to passport.xxx.com will fail because passport is already in a logged-in state and will not allow the login page to open by default, preventing soft from logging in via passport. You have to log out of passport first and then log in again so that both passport and soft can be logged in. The implementation process is as follows:

Configure the following in conf.php on passport.xxx.com:

/* SESSION and COOKIE configuration */
‘SESSION_OPTIONS’       =>  array(‘domain’=>’.hmwis.com’), // session configuration array, supports parameters such as type name id path expire domain
‘SESSION_PREFIX’ => ‘passport_home’, // session prefix
‘COOKIE_PREFIX’  => ‘passport_home_’, // Cookie prefix to avoid conflicts

Configure the following in conf.php on soft.xxx.com:

/* SESSION and COOKIE configuration */
‘SESSION_OPTIONS’       =>  array(‘domain’=>’.hmwis.com’), // session configuration array, supports parameters such as type name id path expire domain
‘SESSION_PREFIX’ => ‘soft_home’, // session prefix
‘COOKIE_PREFIX’  => ‘soft_home_’, // Cookie prefix to avoid conflicts

This way, when printing the $_SESSION from passport.xxx.com and soft.xxx.com respectively, they are completely identical:

Shared print result of $_SESSION in .xxx.com
Shared print result of $_SESSION in .xxx.com

Finally, implement the synchronous login state processing in soft.xxx.com:

Implementing synchronous login state processing in soft.xxx.com
Implementing synchronous login state processing in soft.xxx.com

is_login() is the current application’s logged-in user ID; is_passport_login() is the passport’s logged-in user ID; if it is in a logged-out state, the value is 0;

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系