Sarah
Sarah

Reputation: 13091

Session cookies working under subdomain

How to get session cookies working / to be accessible under domain and subdomains?

Upvotes: 4

Views: 1646

Answers (3)

jb1785
jb1785

Reputation: 742

I'm assuming you are using setcookie(). If so just set the cookie for ".domain.com".

setcookie("testcookie", "1", 0, "", ".domain.com");

Upvotes: 1

RDL
RDL

Reputation: 7961

For session cookies you need to override the cookie params:

So you can either use:

ini_set('session.cookie_domain', '.website.com');

or

session_set_cookie_params(0, '/', '.website.com');

The '.' in front makes it accessible under the domain and the subdomains.

Note: you will have to delete all existing cookies from your browser for the domain you're working with so they can be re-initialized properly to work.

Upvotes: 3

bhamby
bhamby

Reputation: 15499

If you set the cookie for the "top" domain (example.com), the cookie will also apply to subdomains (sub.example.com, another.example.com).

As an aside, this is the reason why some larger companies use completely separate domains to serve static stuff, like stackoverflow uses http://sstatic.net/

Upvotes: 1

Related Questions