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

- 1,650
- 2
- 21
- 31

- 187
- 7
-
1By "different projects" do you mean different domains? – DFriend Oct 04 '17 at 17:05
-
Check for different domains, subdomains, encryption_key, sess_driver, sess_cookie_name, cookie_prefix, cookie_domain, cookie_path, etc – Erich García Oct 09 '17 at 17:47
-
I got the problem. Thanks – Farhad Misirli Oct 10 '17 at 08:43
-
2Already answered this question. https://stackoverflow.com/questions/5783595/codeigniter-session-cookie-domain – Oct 12 '17 at 11:09
-
@FerhadMisirli If you solved the problem, please post the answer here. – BudwiseЯ Jan 21 '18 at 02:22
3 Answers
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.

- 81
- 1
- 4
-
If the contribution is not explained, please, just add as comment. – David García Bodego Oct 16 '19 at 10:37
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.

- 3,875
- 30
- 32
There are two ways to go about this.
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.
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.

- 166
- 5