Welcome to WebmasterWorld Guest from 23.20.82.60

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Get domain from gethostbyaddr?

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

Junior Member

5+ Year Member

joined:Sept 10, 2006
posts:98
votes: 0


IP: 100.100.100.100
Hostname: 100-100-100-100.verizon.net

How do I extract the domain from the host name?

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

Junior Member

5+ Year Member

joined:Sept 10, 2006
posts:98
votes: 0


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 July 19, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 22, 2005
posts: 185
votes: 0


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";