Sharing session data to all subdomain codeigniter
Asked Answered
G

2

6

I am trying to use one session data for all of my subdomains.

I have created a subdomain in cpanel like this : *.mydomain.in

and my *.mydomain.in uses the same path as my mydomain.in example:

mydomain.in uses path on my server: /public_html/mydomain.in

*.mydomain.in uses path on my server: /public_html/mydomain.in

Now the problem is every time I visit the site it's creating a different session. For example:

I visit mydomain.in .... it creates a session.

I visit example.mydomain.in .... it creates a different session

I again visit mydomain.in ... it creates a different session.

my codeigniter config file:

$config['encryption_key'] = 'MY-SECRET-KEY-HERE';

$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 0;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;

$config['cookie_prefix'] = "";
$config['cookie_domain'] = ".mydomain.in";
$config['cookie_path'] = "/";
$config['cookie_secure'] = FALSE;

Any help or suggestion would be a great help. Thanks in advance.

Gittern answered 23/3, 2014 at 8:42 Comment(0)
G
11

Here's how I solved the issue.

 $config['sess_cookie_name'] = 'ci_session';
 $config['sess_expiration'] = 0;
 $config['sess_expire_on_close'] = TRUE;
 $config['sess_encrypt_cookie'] = TRUE;
 $config['sess_use_database'] = TRUE;
 $config['sess_table_name'] = 'ci_sessions';
 $config['sess_match_ip'] = TRUE;
 $config['sess_match_useragent'] = FALSE;
 $config['sess_time_to_update'] = 300000000;


 $config['cookie_prefix'] = "etc_anything_";
 $config['cookie_domain'] = ".mydomain.in";
 $config['cookie_path'] = "/";
 $config['cookie_secure'] = FALSE;
Gittern answered 23/3, 2014 at 9:19 Comment(6)
how do i give for multiple sub domain. That is I have abc.book.com, def.book.com, pop.book.comSocrates
this works for any domain, you can try anything, ex: abc.book.com xyz.book.com both will work..Gittern
what should i give in $config['cookie_prefix'] = "mydomain_";Socrates
your domain name ex: book_ or whatever you want... but cookie_domain should be your real domain name ex: .website.com for production...Gittern
TL;DR you only have to set $config['cookie_domain'] = '.mydomain.com';Anachronous
These CodeIgniter settings are now outdated. Some have been deprecated. Also, the answer provides no explanation about the OP's settings and instead just offers some settings which happen to work. More explanation would make this clearer.Phocis
M
1

Mohamed Sufian's answer is largely correct, but note that as per the Session Docs, the cookie_prefix setting is ignored by the session driver. If you want to have an instance specific cookie name you will need to edit the sess_cookie_name configuration option.

For example:

 $config['sess_cookie_name'] = 'my_prefix_ci_session';

 $config['cookie_prefix'] = "my_prefix_"; // Only relevant for non-session cookies
 $config['cookie_domain'] = ".example.com";
 $config['cookie_path'] = "/";
Missing answered 28/3, 2022 at 16:59 Comment(3)
Hey, thanks for the suggestions, I now use different things to easily manage all my work, and I also use CI4 with Django support, furthermore, the selected answer was old and it was for CI3, again Thanks you have given your important time :-)Gittern
Alas, I'm still supporting a CI3 codebase. I wish that I weren't, but I figure there may be some unfortunate souls out there who need to know this.Missing
I still have many projects running using CI3 & and I still use CI3 because I know & understand that at least I'll be receiving security updates, ThanksGittern

© 2022 - 2024 — McMap. All rights reserved.