简体   繁体   English

在Codeigniter项目之间共享会话存储

[英]Share session storage between codeigniter projects

There are two different projects which have developed on codeigniter. 在Codeigniter上开发了两个不同的项目。 I want to share session storage between these projects. 我想在这些项目之间共享会话存储。 $config['sess_cookie_name'] = 'ci_session'; config file is same in both. 两者的配置文件相同。 But when I refresh one of them, all data automatic remove from another project. 但是,当我刷新其中一个时,所有数据都会自动从另一个项目中删除。 I can't solve this problem. 我不能解决这个问题。 #help #救命

Store sessions in a central database; 将会话存储在中央数据库中; use the same session table for different projects. 对不同的项目使用相同的会话表。

See https://codeigniter.com/user_guide/libraries/sessions.html#database-driver 请参阅https://codeigniter.com/user_guide/libraries/sessions.html#database-driver

Addendum: 附录:

Codeigniter (CI) employs a number of ways to store sessions. Codeigniter(CI)采用多种方式来存储会话。 Flat file, database, Redis etc. Default is flat file; 平面文件,数据库,Redis等。默认为平面文件。 ie; stores session data within the filesystem. 将会话数据存储在文件系统中。

It is also possible to store the sessions in database tables. 也可以将会话存储在数据库表中。 This method facilitates both multi server and multi project installations. 此方法有助于多服务器和多项目的安装。

application/config/config.php does have a section named "Session Variables". application / config / config.php确实有一个名为“会话变量”的部分。

$config['sess_driver'] = 'database'; $ config ['sess_driver'] ='数据库'; <- use database <-使用数据库

$config['sess_save_path'] = 'ci_sessions'; $ config ['sess_save_path'] ='ci_sessions'; <- use this table <-使用此表

shared usage of this table for session storage among different projects will solve the problem. 在不同项目之间共享此表用于会话存储将解决此问题。

The solution here as found: Cross-Domain Cookies is to create a single login portal and then redirect users to the relevant areas from there. 此处找到的解决方案: 跨域Cookie是创建单个登录门户,然后将用户从那里重定向到相关区域。 It isn't possible to share cookies (the basis for sessions) across domains so you need to find an alternative solution. 无法跨域共享Cookie(会话的基础),因此您需要找到替代解决方案。 Centralising your login and verification process should solve that. 集中登录和验证过程应该可以解决该问题。

There are two ways to go about this. 有两种方法可以解决此问题。

  1. The first is to actually store the session in your database. 首先是将会话实际存储在数据库中。 You keep the cookie name in the $_SESSION , and submit that to the database to retrieve the session data. 您将cookie名称保留在$_SESSION ,并将其提交给数据库以检索会话数据。 When moving between machines you can transmit the cookie name in the superglobal of your choice: $_POST or $_GET , but most likely $_GET . 在机器之间移动时,可以在您选择的超全局变量中传输cookie名称: $_POST$_GET ,但很可能是$_GET The problem with this approach is that you need to read and write from the database quite a lot: 这种方法的问题在于,您需要从数据库中进行大量读写操作:

    • read once to retrieve the session on each request. 读取一次即可检索每个请求的会话。 You will need to do this, because you may not be able to guarantee that another machine has not updated the session since the user last refreshed the page. 您将需要执行此操作,因为自用户上次刷新页面以来,您可能无法保证另一台计算机没有更新会话。
    • write once to store the session after each request. 在每个请求之后只写一次以存储会话。 This defeats much of the purpose of having a PHP session cache on a single machine, but is necessary to keep sessions synchronized between machines. 这没有达到在单个计算机上拥有PHP会话高速缓存的许多目的,但是必须保持会话在计算机之间同步。
  2. The alternative is to use that same session key, and keep local $_SESSION data as before, but procedurally generate the session as necessary. 替代方法是使用相同的会话密钥,并像以前一样保留本地$_SESSION数据,但根据需要在过程上生成会话。 The difference now is that the sessions on each machine will not be truly synchronized, but instead generated with the same logic. 现在的区别是,每台计算机上的会话将不会真正同步,而是使用相同的逻辑生成。

Most users will not notice the processing penalty over a single local session as your machines create and update their own local sessions, however they will possibly notice routine database calls attached to their page requests. 当您的计算机创建和更新自己的本地会话时,大多数用户不会注意到单个本地会话的处理代价,但是,他们可能会注意到附加到其页面请求的例行数据库调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM