dongping4461 2019-07-30 13:25
浏览 66

Cakephp 3:跨多个应用程序的共享数据库会话

I have two Cakephp 3 apps that save their sessions on the same "sessions" table on the same database. I've been trying to set it up so that they share the same sessions, so that when I'm logged in to either one, I'm automatically logged in to the other.

I have this in both of their app.php files:

'Session' => [
    'defaults' => 'database'
],

And this is the "sessions" table they're both using: enter image description here

Logging into both results in them having different entries in the table.

Thanks in advance for any suggestions.

  • 写回答

1条回答 默认 最新

  • dousi4148 2019-07-30 17:57
    关注

    While this could theoretically be possible, note that since Database-stored sessions aren't thread locked multiple apps could be writing to the session at the same time. Lots of data is stored in the session data (all the Auth information) so you'll need to be careful to ensure user data is handled uniformly across all apps.

    But generally you'll want to create a new Datasource that will contain the shared "sessions" table (ex. named shared_sessions, in app.php:

    'Datasources'    => [ 
        'shared_sessions'   => [
            'className'        => 'Cake\Database\Connection',
            'driver'           => 'Cake\Database\Driver\Mysql',
            'persistent'       => false,
            'host'             => 'localhost',
            'port'             => '3306',
            'username'         => 'sessions_user',
            'password'         => '*******',
            'database'         => 'SHARED_SESSIONS',
            'timezone'         => 'UTC'
        ],
    

    If you don't already have a SessionsTable, create it and specify the alternative connection information:

    class SessionsTable extends Table
    {
        /**
         * @return string
         */
        public static function defaultConnectionName()
        {
            return 'shared_sessions';
        }
    
        ....
    

    You may still need to handle differences in other Auth data depending on what each app does to set/manipulate it, you may need to do some centralized storage in the shared database, but also store Auth data on an app level, using a Custom Storage Handler.

    See:

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度