views:

45

answers:

3

Hi

I am using a session to maintain some variables between different pages on my site. While it works fine when I move from www.mysite.com/a to www.mysite.com/b, the session variables are lost if I move from mysite.com/a to www.mysite.com/b

Any idea how I can make sure the session is maintained irrespective of www or no www?

The website appends a www as it navigates to different pages but the pages are accessible without that as well. So if someone is manually typing in an address and omit that part, and then navigate to page b (by clicking on a link or submitting a form), the session variables are lost.

Thanks for any help.

+2  A: 

you need to make sure the cookie is being set to .example.com not www.example.com using session_set_cookie_params, specifically parameter 3 - the domain in the documentation

tobyodavies
Setting the domain to ".example.com" would make more sense if he wants the session values to be available under both domains.
mellowsoon
yes, that's what i meant! edited, thanks
tobyodavies
A: 

The session ID is stored in a cookie. Cookies don't transfer over from subdomain to subdomain (e.g. www.somesite.com to somesite.com). You can change the cookie params with session_set_cookie_params() or in php.ini

Jonathan Kuhn
+4  A: 
Gabi Purcaru
+1 - this is the cleaner way to do it, assuming www.mysite.com is the same site logically as mysite.com
tobyodavies
Thanks. I'll go with this. Just one question - If I pass a parameter with a URL (that does not have a www), will the redirect pass the parameter to the www URL automatically?
lostInTransit
don't know for sure, but the second version should.
Gabi Purcaru