8

I need to make a script where it would remove the subdomain from $_SERVER['SERVER_NAME'] to use it on the domain option of the setcookie function to allow the access of the cookie on all possible subdomains.

For example, let's say I have

function strip_out_subdomain($domain)
{
    //do something to remove subdomain
    return $only_my_domain;
}
$domain = strip_out_subdomain($_SERVER['SERVER_NAME']);
setcookie('mycookie', '123', time()+3600, '/', $domain);

The main problem here is I don't know the pattern for my domain. It could be something like:

  • www.mydomain.com
  • subdomain.mydomain.com
  • subdo.mydo.co
  • subdo.subdo.mydomain.com
  • subdo.subdo.mydo.co.uk
  • etc.

Thank you

Stephanie

Stef
  • 523
  • 2
  • 7
  • 13
  • 1
    You will need to manually define the pattern. There is no clear rule (a TLD can consist of one part `.com` or two `.co.jp`) there is a question on SO that has a list of most of them but I can't find it right now – Pekka Mar 16 '11 at 16:33
  • 2
    This came up yesterday too. http://stackoverflow.com/questions/5319296/php-url-parsing-disecting - there's a comprehensive list (mozilla maintained) in the comment at the end. – mario Mar 16 '11 at 16:35
  • @Mario: thank you for your link. I see the problem is popular and not possible to solve easily. – Stef Mar 16 '11 at 16:40
  • See also http://stackoverflow.com/questions/3853338/remove-domain-extension/3853473#3853473 – Gumbo Mar 16 '11 at 17:41
  • possible duplicate of [PHP Regex for extracting subdomains of arbitrary domains](http://stackoverflow.com/questions/4304527/php-regex-for-extracting-subdomains-of-arbitrary-domains) – outis Apr 01 '12 at 21:34

4 Answers4

4

This is a regex style of removing sub domain part from the full domain name.

The

.*?

operator makes the wildcard matching to be ungreedy so that it matches the first dot.

function strip_out_subdomain($domain)
{
    $only_my_domain = preg_replace("/^(.*?)\.(.*)$/","$2",$domain);
    return $only_my_domain;
}
Raj
  • 22,346
  • 14
  • 99
  • 142
3

Is it possible to define a variable in the server configuration (httpd.conf, .htaccess)? It requires a bit of additional initial administration, but could at least be done in a central location.

I was able to set a variable in Apache

SetEnv MY_DOMAIN mydomain.com

which could be consumed in PHP:

$_SERVER['MY_DOMAIN']
KDrewiske
  • 1,539
  • 1
  • 16
  • 17
2

You can query the Alexa service with cURL and extract the hostname without subdomain:

function hostname($domain) {

$querystring = 'http://xml.alexa.com/data?cli=10&dat=nsa&ver=quirk-searchstatus&uid=19700101000000&userip=127.0.0.1&url='.urlencode($domain);
$ch = curl_init();
$user_agent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt ($ch, CURLOPT_URL, $querystring);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
$data= curl_exec($ch);
curl_close($ch);

preg_match('/\<POPULARITY URL="(.*?)" TEXT="(.*?)" SOURCE="(.*?)"\/\>/Ui',$data,$extract);
$hostname = str_replace('/', '', $extract[1]);

return($hostname);
}
awawjerh
  • 214
  • 1
  • 2
  • 12
1

I know it's years later, but why not go like this:

$dom='a.b.c.d.co.jp';
$sub=preg_replace("/.*?([^\.]+)(\.((co\.\w+)|\w+))$/i",'\1\2',$dom); //strip subdomains

this prints d.co.jp

where .*?([^\.]+)(\.((co\.\w+)|\w+))$ would mean:

.*? lazy (so it does not grab the main domain) match all characters until what follows

([^\.]+) match the group of characters that don't contain a dot (i.e. the main domain or next-of-top domain) (the + making sure there is at least one character of the class) and return it later to \1

(\.((co\.\w+)|\w+)) match the TLD with its preceding dot whether it is a .co.something or .something and give it back via \2; the plus sign does the same here

$ anchor everything to the end of the string so we can go all the way from TLD to the left to the subdomain parts, no matter how much of them there are

P.S. I don't know if there are any other two-part TLDs but they can be added as well. A fast skim through https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains tells me no, but if there are any I guess they're not that much.

GGets
  • 416
  • 6
  • 19