【问题标题】:laravel share session / cookie data between laravel and legacy PHP (subdomain)laravel 在 laravel 和遗留 PHP(子域)之间共享会话/cookie 数据
【发布时间】:2022-11-05 03:35:32
【问题描述】:
我在 domain.com 上运行 laravel 应用程序作为某种主应用程序,在子域 app2.domain.com 上运行另一个(不是基于 laravel 的)PHP 应用程序,如何在两个域之间共享 cookie/会话数据?
我需要将登录用户的某种用户模型属性从 laravel 应用程序共享到我的 app2.domain.com 应用程序。
Laravel 会话已经存储在 redis 中,因此很容易访问驱动程序本身。
也许有人在过去已经实现了这个/关于如何在 laravel 和另一个基于子域的 PHP 应用程序之间共享会话/cookie 数据的任何示例?
【问题讨论】:
标签:
php
laravel
subdomain
session-cookies
【解决方案1】:
您可以使用以下过程进行此操作:
确保用于 laravel 安装的 .env 中的环境变量具有与 Php 应用程序相同的设置:(您也必须相应地对您的独立设备进行调整,但我不能在没有看到代码的情况下告诉您代码)
#APP_KEY is used to encrypt the session data so must be the same and the standalone must use laravels algorithm of hashing it
APP_KEY=base64:'YOUR KEY '
#SESSION_COOKIE must be the same, this is normally not set and defaults to APP_NAME+"_session' which is a problem if your apps have different names
SESSION_COOKIE=laravel_session #Same for php standalone
#The SESSION_DOMAIN defaults to null which causes the CORS scope to limit to the subdomain level, in my testing must start with leading dot.
SESSION_DOMAIN=.rootdomain.com #Same for php standalone
就我而言,我还将硬编码的 config/session.php 变量更改为:
'http_only'=>false,//not necessary but helpful in such applications
'samesite'=>'lax',//newer laravel will allow you to set 'none' but beware of security issues