duanraa1984 2018-03-12 09:44
浏览 301

Laravel多个跨域子会话共享

I have the following 4 sub-domains for my project:

www.mysite.com - public site, published pages etc.

my.mysite.com - normal users log in to this domain to create pages and other stuff

company.mysite.com - Company hasMany Employees that log in to this sub-domain to manage their pages and other stuff

admin.mysite.com - Admins of the site log in here to manage everything

--

Typically, I want to keep all 4 sub-domains separate. So a user logged in to my. should not be able to view company. unless they login their as well. I have managed to do this by creating the following middlewares:

auth:my

auth:company

auth:admin

Using the above I have different login views/routes etc. for the different types of users working correctly.

--

I needed a way to share sessions across the subdomains because when a user creates a Page on the my., they can publish it and it shows on the public www. (www.mysite.com/my-page)

What I needed is that when the User who owns the Page and is logged in to my., visits their own page on the www. sub-domain, an Edit button to show.

I managed to do this by sharing sessions across my subdomains by making the following change:

config/session.php

domain => env('SESSION_DOMAIN', '.mysite.com')

--

However, this messes up the logins for company. and admin., because when a User logs in to my., the session is shared across the sub-domains.

How do I share the sessions across the sub-domains but group them so something like:

domain => [
    ['www.mysite.com', 'my.mysite.com'], 
    ['www.mysite.com', 'company.mysite.com'], 
    ['www.mysite.com', 'admin.mysite.com'], 
]
  • 写回答

1条回答 默认 最新

  • dongzhan1570 2018-03-12 11:59
    关注

    The sessions are shared because it's essentially the same application and you're presumably using driver = file or database.

    To fix this, you can have multiple session tables per sub-domain.

    1. Change your config/session.php file and make sure you're using database driver.
    2. Create multiple copies of the sessions table, calling them company_sessions, admin_sessions etc.
    3. Update your code (i.e. config/session.php file) so that the sessions table is based on the subdomain. E.g. create an if-else statement looking for the $_SERVER['HTTP_HOST'] and check it's a particular subdomain. if ( $_SERVER['HTTP_HOST'] == 'admin.mysite.com' ) { $config['table'] = 'admin_sessions'; } The layout of your config/session.php may need to be changed to accomodate overriding the table and returning the correct value.
    4. See if it works!

    There are other ways you can achieve this, but this may be the simplest.

    It may not work as you're trying to share sessions and don't want all sessions to be shared at the same time. You my have to compromise on the functionality and just stick with shared sessions.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。