Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Get domain from gethostbyaddr?



2:30 pm on Jul 16, 2009 (gmt 0)

5+ Year Member

Hostname: 100-100-100-100.verizon.net

How do I extract the domain from the host name?


2:43 pm on Jul 16, 2009 (gmt 0)

5+ Year Member

Found a solution:

$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);

$string = $hostname;
preg_match('@^(?:http://)?([^/]+)@i', $string, $matches);
$host = $matches[1];
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "Domain: " . $matches[0] . "\n";

Well, at least it will get indexed by Google! :)


2:25 am on Jul 19, 2009 (gmt 0)

10+ Year Member

i don't sure, but there seems to be some errors:
escaping some dots . and slashes /
and matching $matches[1]
$string = $hostname; 
preg_match('@^(?:http://)?([^\/]+)@i', $string, $matches);
$host = $matches[1];
preg_match('/[^\.]+\.[^\.]+$/', $host, $matches);
echo "Domain: " . $matches[1] . "\n";

Featured Threads

Hot Threads This Week

Hot Threads This Month