gethostbyname() - php 网络函数
gethostbyname()
(PHP 4, PHP 5, PHP 7)
返回主机名对应的 IPv4地址。
说明
gethostbyname(string $hostname): string返回主机名$hostname对应的 IPv4 互联网地址。
参数
$hostname主机名
返回值
成功时返回 IPv4 地址,失败时原封不动返回$hostname字符串。
范例
简单的gethostbyname()例子
参见
gethostbyaddr()
获取指定的IP地址对应的主机名gethostbynamel()
获取互联网主机名对应的 IPv4 地址列表inet_pton()
Converts a human readable IP address to its packed in_addr representationinet_ntop()
Converts a packed internet address to a human readable representation
If you do a gethostbyname() and there is no trailing dot after a domainname that does not resolve, this domainname will ultimately be appended to the server-FQDN by nslookup. So if you do a lookup for nonexistentdomainname.be your server may return the ip for nonexistentdomainname.be.yourhostname.com, which is the server-ip. To avoid this behaviour, just add a trailing dot to the domainname; i.e. gethostbyname('nonexistentdomainname.be.')
Options for the underlying resolver functions can be supplied by using the RES_OPTIONS environmental variable. (at least under Linux, see man resolv.conf) Set timeout and retries to 1 to have a maximum execution time of 1 second for the DNS lookup: You should also use fully qualified domain names ending in a dot. This prevents the resolver from walking though all search domains and retrying the domain with the search domain appended.
This function says "Returns the IPv4 address or a string containing the unmodified hostname on failure. This isn't entirely true, any hostname with a null byte in it will only return the characters BEFORE the null byte. Results: string 'foobar' (length=7) string 'foo' (length=3)
Important note: You should avoid its use in production. DNS Resolution may take from 0.5 to 4 seconds, and during this time your script is NOT being executed. Your customers may think that the server is slow, but actually it is just waiting for the DNS resolution response. You can use it, but if you want performance, you should avoid it, or schedule it to some CRON script...
For doing basic RBL (Real Time Blacklist) lookups with this function do: Tomas V.V.Cox
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)For the "dig" solution, here is a better one: Using tail because dig, even in the +short form, may return CNAME entries first, and also may return many, many entries (see "dig google.com"). Usig the dot at the end of the host to not perform domain searches. This works also when $host = IP address already Will return empty string in case o resolve failure (invalid hostname)Always add the root . at the end or your server can add it's own suffix. example: abc.com could become abc.com.yourcompany.com if you don't use abc.com. I also find this function a litle bit limited because it doesn't tell you if the resolve failed. TO adress all those issues I use : function host2ip($host) { $host=trim($host.'.'); // clean and add root . $ip= gethostbyname($host); if($ip==$host) $ip='';// empty IP if there is no ip return $ip; }referring to ralphbolton at mail2sexy dot com comment: (at least in 5.2.0 + djbdns-dnscache) gethostbyname does not really seem to cache entries. If somebody notices a speed-up after the second lookup of the same domain - that's most likely your dns-cache itself, not some php-internal dns-cache. It does cache the entries in your /etc/resolv.conf (e.g. what dns to use) so I agree with him, that stopping and starting apache, will reload the resolv.conf.If name resolution fails with apache2, mod_chroot and php5, add LoadFile /lib/libnss_dns.so.2 to the mod_chroot config.When using PHP and Apache in a chroot environment on RedHat Linux, I have found that I need to bind-mount /var/run/nscd to get this to work. Apparently, the socket in that directory is needed for all the DNS things.One note about using gethostbyname() for checking email address domains: If the name doesn't resolve, follow up with getdnsrr() and make sure they don't have an MX entry before returning an error. It is possible for a domain name not to have an A record, but still have an MX entry.In PHP4 you can use gethostbyname() but I have found this unreliable when doing lookups on entries that return A records on the private network. PHP5 has a much better routine -- dns_get_record(). If you are stuck with PHP4 or don't want to upgrade you can use dig:Function returns boolean:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)