PHP Session Sharing Among Subdomains

Wed, Mar 3, 2010 One-minute read

PHP supports sharing sessions among subdomains which can be very useful for sharing cookies and sessions among multiple web applications.

I wanted my users to remain logged in while navigating from www.example.com to site1.example.com. By default PHP will treat these domains as two separate sites and users would have to login to each subdomain.

All you have to do is to either set the following setting in your php.ini file:

session.cookie_domain = .example.com

Or, set it from within your code before you create your session:

ini_set("session.cookie_domain", ".example.com");
session_start();