sniper
sniper

Reputation: 2943

How do I use session variables across multiple sub-domains?

I have been losing my session variables rather consistently when I click on the link from our websites notification email. After breaking my head for a long time on this, I today realized that www.domain-name.com does not contain the session variables while domain-name.com does!!

Why does this happen? And what do I do to set things right(php-apache)?

Upvotes: 4

Views: 1162

Answers (4)

Nican
Nican

Reputation: 7935

The session ID is stored in a cookie, and in the cookie can be specified how it should react over domain names.

Take a look at PHP's setcookie documentation.

You can change PHP's session cookie configuration with:

ini_set("session.cookie_domain", ".mydomain.com");

Upvotes: 2

SLaks
SLaks

Reputation: 887275

Sessions are based on cookies, which are per-domain.
www.domain.com is a different domain than domain.com, so their cookies are kept separate.

Standard practice is to choose one variant and 301 redirect the other variant to the preferred one.

Upvotes: 3

bignose
bignose

Reputation: 32279

There's nothing technically special about ‘www’. The domain ‘domain.com’ is distinct from ‘www.domain.com’; if you want to associate them, that needs to be explicit somewhere, usually in the HTTP server configuration.

Upvotes: 1

Related Questions