Best way to detect if request came from a different domain or a sub domain
<p>I'm building an application that uses sub domains and custom domain names that sit in the database for users, so if a request comes from another domain I'll check from the database if that custom url is indeed there or when the request comes from a subdomain, I'll check if that's there. If it is I do my stuff.</p>

<p>Consider this a simple example of I'm looking for:</p>

<pre><code>if(is_user_request())
{
    $url = get_url();
    // assuming that get_url() magically decides whether to output ..
    // a custom domain (http://domain.tld)
    // or a subdomain's first part (eg. "this".domain.tld)
}
else
{
    // otherwise it's not a sub domain nor a custom domain,
    // so we're dealing with our own main site.
}
</code></pre>

<p>Now before you go ahead assuming that because I have 0 rep, I'm here asking for "teh codes". I have a completely working way of doing this, which is the following:</p>

<pre><code>// hosts
$hosts = explode('.', $_SERVER['HTTP_HOST']);

// if there is a subdomain and that's under our $sitename
if(!empty($hosts[1]) AND $hosts[1] === Config::get('domain_mid_name'))
{
    $url = $hosts[0];
    $url_custom = false;
}

 // if there is no subdomain, but the domain is our $sitename
 elseif(!empty($hosts[0]) AND $hosts[0] === Config::get('domain_mid_name') AND !empty($hosts[1]) AND $hosts[1] !== Config::get('domain_mid_name'))
    {
    $url = false;
    $url_custom = false;
}

// otherwise it's most likely that the request
// came from a entirely different domain name.
// which means it's probably $custom_site
else
{
    $url = false;
    $url_custom = implode('.', $hosts);
}

if($url)
{
    return $url;
}

if($url_custom)
{
    return $url_custom;
}
</code></pre>

<p>However, I'm sure there are better way of doing this. Because first of all, HTTP_HOST does not include 'http://', so I need to add that manually and I'm pretty sure this entire if, else thing is just an overkill. So, people smarter than me, enlighten me, please.</p>

<p>Oh and, no .. I do not have pre-defined sub-domains. I have a simple wildcard *.domain.tld set up, so all sub-domains go to the main script. I'm just saying this because from my search for a solution I found numerous answers that suggested to manually create a sub-domain, which is not even remotely related to what I'm asking, so let's skip that subject.</p>
