4

Actually I am trying to get the sub-domain URL using php. I write code below:

$sub_url = explode('.', $_SERVER['HTTP_HOST']);
$suburl = $sub_url[0];

For Example: if sub domain URL is like my.example.com the code above is giving me my which is good but if there is no sub domain then my code will return example which is not good.

I want anything before the first dot even www but if there is nothing like if URL is example.com then I want a blank value instead of example.

2
  • 2
    if (count($sub_url) > 2) ? Keep in mind, one.two.three.domain.com could also occur. Commented Jul 6, 2018 at 15:53
  • Great point and I have done that using the way you said. Commented Jul 6, 2018 at 16:03

3 Answers 3

14

Here's a one-liner to get the subdomain:

$subdomain = join('.', explode('.', $_SERVER['HTTP_HOST'], -2))

explode with the limit parameter of -2 will split the string on each dot, ignoring the last two elements. If there are two or fewer elements, it returns an empty array.

join will assemble the resulting array back into a dot delimited string. In case you have multiple subdomains set, e.g. foo.bar.domain.com will return foo.bar.

If there is no subdomain, it will return an empty string.

Sign up to request clarification or add additional context in comments.

1 Comment

Worked for me if I change the parameter to: 1
1

I'd assume you can just check the size of the array, assuming it was always the same size, if it was any larger you may run into problems.

$sub_url = explode('.', $_SERVER['HTTP_HOST']);

if (sizeof($sub_url) > 2) {
    $suburl = $sub_url[0];
} else {
    $suburl = null;
}

Comments

0

You should notice that, there are also websites with second level domains, for example co.uk, com.cn. If you can make sure, that your code will not be used on such website, you can simple use the answer of DarthJDG or check count($sub_url) > 2. But if not, you need to check the domain name.

$secondLevelDomainsList = str_getcsv(file_get_contents('https://raw.github.com/gavingmiller/second-level-domains/master/SLDs.csv'), PHP_EOL);
foreach($secondLevelDomainsList as &$item) {
    list($tld, $stld) = str_getcsv($item, ",");
    $item = $stld;
}    
$urlParts = explode('.', $url);    
$challenger = '.' . join('.', array_slice($urlParts, -2, 2, true));    
$length = in_array($challenger, $secondLevelDomainsList) ? -3 : -2;    
$subDomainName = join('.', array_slice($urlParts, 0, $length, true));

The list of second level domains in format of CSV from gavingmiller/second-level-domains github repository is used for the test, if the domain is a second level domain.

2 Comments

And using any list like that gives you two problems: what happens if you can not access it for whatever reason, and how can you be sure it is up to date (last commit there was 4 years ago). Here is another one kept more up to date because used by all browsers: publicsuffix.org/list/public_suffix_list.dat (read publicsuffix.org for explanations on what is it and how it is used).
Yes, there will be such problems. But to the first problem, a local copy or cache can be used. To the second, the git repository in the code is a simple example, my purpose is to notice, that the second level domain should also be considered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.