且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 PHP 中的同一服务器上跨多个域共享会话

更新时间:2023-11-22 17:18:40

为了验证我没有错,您需要在所有应用程序之间共享用户会话.

Just to verify I am not wrong, you need to share user session across all your applications.

正如上面所说的,您可以使用上面的 4 个选项.但是,我想关注第一个选项,即将会话放入数据库,并建议另一个选项,将会话保存在共享目录或服务器中.

As rightly said above, you may use 4 of the options above. However, I would like to focus on first option which is putting session in DB and would like to suggest another option as keeping sessions in shared directory or server.

  1. DB 中的会话 - 您问题的答案(如果来自同一网络的其他用户使用相同的用户代理命中怎么办?)是您将拥有不同的会话id 的值来唯一标识表中的每一行.因此,无需担心.但缺点是,每次都需要数据库连接并触发查询时,会话被初始化,即每个页面.

  1. Sessions in DB - The answer to your question (What if other user from same network with same user agent hits?) is you will have different session id's value to uniquely identify each row in Table. So, no need to worry about it. But the disadvantage is, each time DB connection would be required and a query would be fired, when session is initialized i.e. for every single page.

共享目录/服务器中的会话 - 以所有应用程序都在共享位置存储会话的方式配置所有应用程序.共享位置可以是目录或安全服务器.这可以通过使用 session_set_save_handler 轻松实现.

Sessions in shared directory/server - Configure all your applications in a such a manner that all applications store session at shared location. Shared location can either be a directory or a secured server. This can easily achieved by using session_set_save_handler.