note 108434 added to function.gethostbyname

From: Date: Thu, 26 Apr 2012 08:48:16 +0000
Subject: note 108434 added to function.gethostbyname
Groups: php.notes 
Request: Send a blank email to php-notes+get-187356@lists.php.net to get a copy of this message
Second level ccTLDs (like .co.uk, .me.uk, etc.) can confuse the taks to find out the "base" domain name of a hostname or IP address. Say you have two hostnames: www.ebay.co.uk and www.ebay.com. You cannot get the base domain name ebay.co.uk and ebay.com just with getting the last two parts seperated with dots. Here is a simple solution without the need to maintain a second level ccTLD list: <?php function get_base_domain_by_name ( $name ) { if ( is_string($name) && preg_match('/^[a-z0-9\-\.]+\.[a-z0-9\-]+$/i', $name) === 1 ) { $parts = array_reverse(explode('.', strtolower($name))); $base = array_shift($parts); foreach ( $parts as $part ) { $base = $part.'.'.$base; if ( ( $addr = gethostbyname($base) ) != $base && preg_match('/^[0-9\.]+$/', $addr) === 1 ) { return($base); } } } return(false); } function get_base_domain_by_addr ( $addr ) { return(get_base_domain_by_name(gethostbyaddr($addr))); } echo get_base_domain_by_name('www.ebay.co.uk')."\n"; echo get_base_domain_by_addr('66.135.200.23')."\n"; // reverse: pages.intl.ebay.com ?> Output: ebay.co.uk ebay.com ---- Server IP: 92.48.74.199 Probable Submitter: 80.77.114.203 ---- Manual Page -- http://www.php.net/manual/en/function.gethostbyname.php Edit -- https://master.php.net/note/edit/108434 Del: integrated -- https://master.php.net/note/delete/108434/integrated Del: useless -- https://master.php.net/note/delete/108434/useless Del: bad code -- https://master.php.net/note/delete/108434/bad+code Del: spam -- https://master.php.net/note/delete/108434/spam Del: non-english -- https://master.php.net/note/delete/108434/non-english Del: in docs -- https://master.php.net/note/delete/108434/in+docs Del: other reasons-- https://master.php.net/note/delete/108434 Reject -- https://master.php.net/note/reject/108434 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#187356) next »