I have to create a script where it might take away the subdomain from $_SERVER['SERVER_NAME'] to apply it to the domain use of the setcookie function to permit the access from the cookie on all possible subdomains.

For instance, let us say I've

function strip_out_subdomain($domain)

$domain = strip_out_subdomain($_SERVER['SERVER_NAME'])

setcookie('mycookie', '123', time()+3600, '/', $domain)

The primary problem here's I'm not sure the pattern for my domain. It may be something similar to:

  • world wide web.mydomain.com
  • subdomain.mydomain.com
  • subdo.mydo.co
  • subdo.subdo.mydomain.com
  • subdo.subdo.mydo.co.united kingdom
  • etc.



Can you really define a flexible within the server configuration (httpd.conf, .htaccess)? It takes a little of more initial administration, but tend to a minimum of be completed in a convenient location.

I could set a flexible in Apache

SetEnv MY_DOMAIN mydomain.com

that could be consumed in PHP:


This can be a regex type of getting rid of sub domain part in the full domain title.



operator helps make the wildcard matching to become ungreedy to ensure that it matches the very first us dot.

function strip_out_subdomain($domain)