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.

CerebralCortexan
  • 299
  • 2
  • 17
Mohammed Sufian
  • 1,743
  • 6
  • 35
  • 62

2 Answers2

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;
Mohammed Sufian
  • 1,743
  • 6
  • 35
  • 62
  • how do i give for multiple sub domain. That is I have abc.book.com, def.book.com, pop.book.com – Gopalakrishnan Feb 19 '16 at 06:24
  • this works for any domain, you can try anything, ex: abc.book.com xyz.book.com both will work.. – Mohammed Sufian Feb 19 '16 at 06:31
  • what should i give in $config['cookie_prefix'] = "mydomain_"; – Gopalakrishnan Feb 19 '16 at 06:34
  • 1
    your domain name ex: `book_` or whatever you want... but `cookie_domain` should be your real domain name ex: `.website.com` for production... – Mohammed Sufian Feb 19 '16 at 06:49
  • TL;DR you only have to set `$config['cookie_domain'] = '.mydomain.com';` – Farzher Sep 21 '17 at 12:18
  • 1
    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. – S. Imp Jan 23 '18 at 20:42
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'] = "/";
Rex Schrader
  • 536
  • 6
  • 17
  • 1
    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 :-) – Mohammed Sufian Mar 29 '22 at 12:15
  • 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. – Rex Schrader Mar 30 '22 at 14:11
  • 1
    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, Thanks – Mohammed Sufian Mar 31 '22 at 12:46