Farhad Misirli
Farhad Misirli

Reputation: 187

Share session storage between codeigniter projects

There are two different projects which have developed on 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

Upvotes: 7

Views: 738

Answers (3)

Kagan Kongar
Kagan Kongar

Reputation: 81

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

Addendum:

Codeigniter (CI) employs a number of ways to store sessions. Flat file, database, Redis etc. Default is flat file; 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".

$config['sess_driver'] = 'database'; <- use database

$config['sess_save_path'] = 'ci_sessions'; <- use this table

shared usage of this table for session storage among different projects will solve the problem.

Upvotes: 1

anEffingChamp
anEffingChamp

Reputation: 166

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. When moving between machines you can transmit the cookie name in the superglobal of your choice: $_POST or $_GET, but most likely $_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.
  2. The alternative is to use that same session key, and keep local $_SESSION data as before, but procedurally generate the session as necessary. 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.

Upvotes: 0

Antony
Antony

Reputation: 4340

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. It isn't possible to share cookies (the basis for sessions) across domains so you need to find an alternative solution. Centralising your login and verification process should solve that.

Upvotes: 0

Related Questions