Hi Everyone,

I got a strange problem with session. Session variables that I define in a page with "www" prefix in the domain name for example http://www.foo.com/page1.php for some reason can't be recognized in page2.php if I remove the "www". But if I use "www" like http://www.foo.com/page2.php, it works just fine.

Can anybody tell me why is this happening? I sure use session_start(). Does anybody in any chance have experienced this?

Using php 5.2.8 on LiteSpeed host.

Any help will be greatly appreciated.

-Max

Recommended Answers

All 10 Replies

This is not strange at all.
Those are two different domains, and sessions stick to one domain for security.

With cookies you can choose domains they will work on, so I'm sure you can look up in the PHP manual for sessions on how to get around having sessions for your second domain too.

This is not strange at all. Those are two different domains

Hello, thanks for your reply. But I'm confused. Does removing the "www" makes it a different domain? Say for example, is http://www.google.com different from http://google.com? I thought they're just the same.

looking forward to your response.

- Max

Yes it is a different domain - setup to show the same content though.

In DNS it is usually setup like this
my.domain.com A 000.000.000
www.my.domain.com CNAME my.domain.com

Unless you use virtual servers in Apache, theres not much more to it.

But in all correctness, it is different domains.
WWW. prefix is nothing special, just some old guy who thought the world would be better if we put www. in front of everything ;)

Technically my problem is not solved yet. But thanks for clearing that up for me. All this time I thought they're the same. I really appreciate your replies. It really helped me.

I guess PHP has an option to automatically share sessions with www. domains aswell - are you using a non-default PHP.ini?
In DNS www. is just as much a subdomain as sub. would be :)

I haven't actually checked.
But if i don't specify www as a sub-domain it autimatically views the original domain without any re-directs

Some websystems enforces www. to be left in or left out.
By checking if the request uri has www., and if not will redirect to it.

This would be a hotfix to your problems, and has no, afaik, sideeffects.

you can set session to work for all subdomain by using ini_set for 'session.cookie_domain'

awesome post..... Solved my problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.