Search code examples
phpcookiessetcookie

Create cookie for subdomains


We have multiple subdomains: es.mysite.com, pt.mysite.com, www.mysite.com depending upon the language needed.

When we do the following, it creates a cookie per subdomain, but we want to create a cookie that can be used for all our subdomains.

setcookie('mycookie', 'en', strtotime('+7 day'), '/')

How would i create a cookie using php7 such that the domain is set to .mysite.com for the cookie's domain value?

Current result:

  1. if i navigate to es.mysite.com, cookie is create with domain = es.mysite.com
  2. if i navigate to pt.mysite.com, cookie is create with domain = pt.mysite.com

Desired Output:

  1. if i navigate to es.mysite.com, cookie is create with domain = .mysite.com
  2. if i navigate to pt.mysite.com, cookie is create with domain = .mysite.com

Solution

  • Add the main domain to the end like this:

    setcookie('mycookie', 'en', strtotime('+7 day'), '/', 'mysite.com');