Skip to content

Instantly share code, notes, and snippets.

@netcarver
Created September 17, 2008 15:18
Show Gist options
  • Save netcarver/11245 to your computer and use it in GitHub Desktop.
Save netcarver/11245 to your computer and use it in GitHub Desktop.
function ign_getDomain() {
$debug = true;
$res = $_SERVER['HTTP_HOST'];
if (false === ip2long($res)) { # This is a normal domain string
$d = explode('.', $_SERVER['HTTP_HOST']);
// $d_copy keeps code simple
$d_copy = $d;
// Make sure the last 2 values look like TLDs (no more than 3 characters). Not bulletproof (.info? .mobi?), but simple.
if ( (count($d) > 2) && (strlen(array_pop($d_copy)) < 4) && (strlen(array_pop($d_copy)) < 4) ) {
$res = join('.', array_slice($d, -3, 3));
}
else {
$res = join('.', array_slice($d, -2, 2));
}
$res .= '.'; # ensure canonical end-of-string
}
else {
# This is an ip address. No action needed
}
if ($debug) dmp( $res );
return $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment