Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to read a cookie from a different sub-domain? If so, how?

Tags:

cookies

I'm currently doing development on a site for a client. This site will be hosted on a subdomain of the client's main site. The client's main site is

www.xyz.com

and the site I'm working on will be hosted at

funds.xyz.com

Anyone who visits www.xyz.com will have a cookie written to their machine which contains data specifying the user's region. I need that data on my site. Is it possible for me to get access to this cookie?

like image 293
DaveDev Avatar asked Mar 14 '11 11:03

DaveDev


People also ask

Can you read cookies from subdomain?

That is, if the domain name in your cookie's domain parameter doesn't start with a period, then it will not let subdomains read that cookie. If it does start with the period, then all subdomains will have full access to that cookie's value. Can only be read by example.com.

How do cookies work in different domains?

Cookies are the go-to method for tracking user information in a web client. First-party cookies (cookies set on the current domain you are browsing) allow tracking for data on a single domain or subdomains, so they will not work across top-level domains.

Can cookies be accessed cross domain?

You cannot share cookies across domains. You can however allow all subdomains to have access.

Can a domain set cookie for another domain?

You cannot set cookies for another domain. Allowing this would present an enormous security flaw.


1 Answers

Yes, so long as the cookie is set for domain ".xyz.com" (note the leading "."). This makes the cookie available to all subdomains of xyz.com

On the other hand, if the domain is "www.xyz.com", the cookie is only visible to the www subdomain.

like image 189
Rob Agar Avatar answered Oct 21 '22 00:10

Rob Agar