Best way to set a first-party cookie for requesting domain and all its subdomains


I have a third-party JS script that is installed on multiple sites.

I’d like to use a cookie for authentication and this cookie should work across each domain’s subdomains as well.

For this to work, I need to reliably get the top-level domain for each incoming request, and store this in the cookie.

One way would be to manually parse the host field stored in the Conn struct and remove any subdomains, but this can be error prone due to the many different shapes of TLDs

Any better solutions?

use this


That’s perfect, thanks

no worries. My Google-fu is legendary.

1 Like