开发者

Keeping session information between a primary domain and a subdomain

开发者 https://www.devze.com 2023-02-05 20:05 出处:网络
I was wondering if it is possible to retain session information between a primary domain and a subdomain. For example, I login on http://www.mydomain.com/ but I want t开发者_如何学Goo use the same ses

I was wondering if it is possible to retain session information between a primary domain and a subdomain. For example, I login on http://www.mydomain.com/ but I want t开发者_如何学Goo use the same session on http://something.mydomain.com/.


Use session_set_cookie_params()

session_set_cookie_params ( $lifetime , '/', '.yourdomain.com');

Note, that the domain starts with a dot. This means, the session cookie is valid for every subdomain!

http://php.net/manual/en/function.session-set-cookie-params.php


Yes, if the session data is accessible from both domains (e.g. same server) and if the same session ID is used for both domains, you can share the same session in both domains.

As for the session ID, if you use a cookie for the session ID, you need to adjust the session cookie’s domain parameter to make the cookie available on both domains (i.e. .example.com for example.com and all its subdomains):

ini_set('session.cookie_domain', '.example.com');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号