PHP function to get the subdomain of a URL

Here’s a one line solution: array_shift((explode(‘.’, $_SERVER[‘HTTP_HOST’]))); Or using your example: array_shift((explode(‘.’, ‘en.example.com’))); EDIT: Fixed “only variables should be passed by reference” by adding double parenthesis. EDIT 2: Starting from PHP 5.4 you can simply do: explode(‘.’, ‘en.example.com’)[0];

Get the subdomain from a URL

Anyone have any great ideas besides storing a list of all TLDs? No, because each TLD differs on what counts as a subdomain, second level domain, etc. Keep in mind that there are top level domains, second level domains, and subdomains. Technically speaking, everything except the TLD is a subdomain. In the domain.com.uk example, “domain” … Read more

Share cookie between subdomain and domain

If you set a cookie like this: Set-Cookie: name=value then the cookie will only apply to the request domain, and will only be sent for requests to the exact same domain, not any other subdomains. (See What is a host only cookie?) Two different domains (e.g. mydomain.com and subdomain.mydomain.com, or sub1.mydomain.com and sub2.mydomain.com) can only … Read more