如何在子域之间使$ _SESSION保持一致? [重复]

I am using session in my website to authentication of user and check if user is logged in or not.

The problem is that if user login using http://example.com and after this user open the website by using http://www.example.com, the session is not available and he has to login again.

What can I do to solve this kind of issue?

</div>

I love how Yahoo explains it.

If your domain is www.example.org, you can host your static components on static.example.org. However, if you've already set cookies on the top-level domain example.org as opposed to www.example.org, then all the requests to static.example.org will include those cookies.

You have two options:

  1. When users go to your website, redirect to www.*
  2. Have cookies effective on the top-level domain

The issue with having cookies work everywhere is that a cookie-less sub-domain will not work on the same domain.

In this case, you can buy a whole new domain, host your static components there, and keep [that] domain cookie-free.