Subdomain Session Sharing Solutions

1. Using a COOKIE to store session_id();

Example:
PHP code for Domain One file:
<?phpsession_start();setcookie("session_id",session_id(),time()+3600*24*365*10,"/",".session.com"); $_SESSION['user_name'] = '梁山良民';echo $_SESSION['user_name'];?>

PHP code for Domain Two file:

<?phpsession_destroy(); session_id($_COOKIE['session_id']);session_start();echo $_SESSION['user_name'];?>
2. Using php.ini’s session.cookie_domain
Note: This is achieved using PHP code without modifying the php.ini configuration file; if needed, you can modify the configuration file in php.ini (in which case this line of code is unnecessary: ini_set(“session.cookie_domain”,’session.com’);)
Example:
PHP code for Domain One file:
<?phpini_set("session.cookie_domain",'session.com');//Note: This parameter must be placed before session_start(), otherwise it will not take effectsession_start();$_SESSION['user_name'] = '梁山良民';echo $_SESSION['user_name'];?>
PHP code for Domain Two file:
<?phpini_set("session.cookie_domain",'session.com');session_start();echo $_SESSION['user_name'];?>

A few special notes for issues you might encounter later, hehe!

First: ini_set(“session.cookie_domain”,’session.com’); must be placed before session_start();

Second: When testing on certain sites, you must pay attention. It might work fine on your local machine, but if there are issues on other machines or servers, you need to check that the ‘session.com’ in ini_set(“session.cookie_domain”,’session.com’); must truly match your domain name! Otherwise, some of your session values will encounter problems!

 

Leave a Comment

Your email address will not be published.