note 70936 added to function.gethostbyname

From: Date: Sun, 05 Nov 2006 02:55:31 +0000
Subject: note 70936 added to function.gethostbyname
Groups: php.notes 
Request: Send a blank email to php-notes+get-119400@lists.php.net to get a copy of this message
gethostbyname and gethostbynamel does not ask for AAAA records. I have written two functions to implement this. gethostbyname6 and gethostbynamel6. I don't believe this issue has been addressed yet. They are made to replace gethostbyname[l], in a way that if $try_a is true, if it fails to get AAAA records it will fall back on trying to get A records. Feel free to correct any errors, I realise that it is asking for *both* A and AAAA records, so this means two DNS calls.. probably would be more efficient if it checked $try_a before making the query, but this works for me so I'll leave that up to someone else to implement in their own work.. the tip is out there now anyway.. Here is the code: function gethostbyname6($host, $try_a = false) { // get AAAA record for $host // if $try_a is true, if AAAA fails, it tries for A // the first match found is returned // otherwise returns false $dns = gethostbynamel6($host, $try_a); if ($dns == false) { return false; } else { return $dns[0]; } } function gethostbynamel6($host, $try_a = false) { // get AAAA records for $host, // if $try_a is true, if AAAA fails, it tries for A // results are returned in an array of ips found matching type // otherwise returns false $dns6 = dns_get_record($host, DNS_AAAA); if ($try_a == true) { $dns4 = dns_get_record($host, DNS_A); $dns = array_merge($dns4, $dns6); } else { $dns = $dns6; } $ip6 = array(); $ip4 = array(); foreach ($dns as $record) { if ($record["type"] == "A") { $ip4[] = $record["ip"]; } if ($record["type"] == "AAAA") { $ip6[] = $record["ipv6"]; } } if (count($ip6) < 1) { if ($try_a == true) { if (count($ip4) < 1) { return false; } else { return $ip4; } } else { return false; } } else { return $ip6; } } ---- Server IP: 203.202.10.60 Probable Submitter: 211.31.229.60 ---- Manual Page -- http://www.php.net/manual/en/function.gethostbyname.php Edit -- https://master.php.net/note/edit/70936 Del: integrated -- https://master.php.net/note/delete/70936/integrated Del: useless -- https://master.php.net/note/delete/70936/useless Del: bad code -- https://master.php.net/note/delete/70936/bad+code Del: spam -- https://master.php.net/note/delete/70936/spam Del: non-english -- https://master.php.net/note/delete/70936/non-english Del: in docs -- https://master.php.net/note/delete/70936/in+docs Del: other reasons-- https://master.php.net/note/delete/70936 Reject -- https://master.php.net/note/reject/70936 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119400) next »