Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

Maintain PHP Sessions Across Root Domain and Sub Domains

Hi Friends,

I was working with a big project recently and It has 5 sub domains along with root domain. In local environment I am complete with everything by making 5 folders inside main project folder. Initially I was not warn by client that we need to make all other 5 sites working as sub domain. My client said me that make other 5 sites in folder inside root domain and users will be same for all 6 sites including root domain. so User can login from any of the website and we need to maintain the same login to all other 5 sites.

As I made folders, I can get the sessions easily on all websites. But later when we were ready to launch, client asked to make sub domains for those inside 5 folders. I made them and when I login from root domain, i did not get session value on sub domain. I was searching for the same solution and everybody saying to use cookie. It will be drastic change if I change it to cookie instead of session. I read the “session.cookie_domain” on PHP website. It looks confusing as it says session and cookie both in statement. After reading whole article, I will be clear about this and here we go how to use PHP Sessions across sub domains and root domain.

Please below line as first line of your all pages. Remember, put this at top above session_start() declaration.

ini_set('session.cookie_domain', '.programmingfacts.com' );

You need to place the same in all root domain files and all sub domains’ files.

Now you will get the session registered on root domain on all sub domains and vice versa.

Isn’t it easy to use PHP Sessions across sub domains, allow php sessions to carry over sub domains, multiple domains sessions, keep sessions on sub domains, keep same session on root and sub domain, passing session data to sub domains.


No Comments Yet.

Leave a Reply

Your email address will not be published.