Sharing PHP SESSION Variables between Multiple Subdomains

As we know that the PHP session ids are saved in Cookies. To make a cookie available in all the sub-domains you need to assign it to the root domain. Then all the sub-domains will get the session id from cookie and PHP can find the session using passed session id.

For implementing  above, you just need to set the session.cookie_domain to the root domain in php.ini file. The session.cookie_domain specifies the domain to set in the session cookie. Default is none at all meaning the host name of the server which generated the cookie according to cookies specification.

<?php
session.cookie_domain = ".example.com"
?>

You can also save below settings in .htaccess file:
<?php
php_value session.cookie_domain .example.com
?>

or can set below settings at start of your PHP script:

<?php
ini_set('session.cookie_domain', '.example.com');
?>


 

 

You may like these posts