PHP 脚本中 DNS_GET_RECORD MX 查找失败
我有一个 PHP 脚本,它使用 get_dns_record
检索并显示通过表单提交的域的特定 DNS 记录。
它工作得非常好,只是处理 MX 记录的部分有点不可靠。有时根本不显示任何 MX 记录(在我知道有这些记录的域上)。如果你刷新2-3次,有时它们就会出现。有时他们不会。
function getDNSRecord($domain1) {
$dns = dns_get_record( $domain1, DNS_ANY );
echo "These are DNS records";
foreach( $dns as $d ) {
// Only print A and MX records
if( $d['type'] != "A" and $d['type'] != "MX" )
continue;
// Print type specific fields
switch( $d['type'] ) {
case 'A':
// Display annoying message
echo "<b>\n" . $d['ip'] . "</b>\n is the Primary A Record for this domain.";
break;
case 'MX':
// Resolve IP address of the mail server
$mx = dns_get_record( $d['target'], DNS_A );
foreach( $mx as $server ) {
echo "This MX record for " . $d['host'] . " points to the server <b>\n" . $d['target'] . "</b>\n whose IP address is <b>\n" . $server['ip'] . "</b>. It has a priority of <b>\n" . $d['pri'] . "</b>\n.";
}
if ( $d['target'] == $domain1 ) {
echo "<div id='mx-status'>There is an issue with this MX Record</div>\n";
} else {
echo "<div id='mx-status'>This MX Record looks fine.</div>\n";
}
break;
}
}
}
I have a PHP script that is using get_dns_record
to retrieve and display specific DNS records for a domain, submitted via a form.
It's working really well, except that the section that handles MX records is a little unreliable. Sometimes no MX Records are displayed at all (on domains I know have them). If you refresh 2-3 times, sometimes they will show up. Sometimes they won't.
function getDNSRecord($domain1) {
$dns = dns_get_record( $domain1, DNS_ANY );
echo "These are DNS records";
foreach( $dns as $d ) {
// Only print A and MX records
if( $d['type'] != "A" and $d['type'] != "MX" )
continue;
// Print type specific fields
switch( $d['type'] ) {
case 'A':
// Display annoying message
echo "<b>\n" . $d['ip'] . "</b>\n is the Primary A Record for this domain.";
break;
case 'MX':
// Resolve IP address of the mail server
$mx = dns_get_record( $d['target'], DNS_A );
foreach( $mx as $server ) {
echo "This MX record for " . $d['host'] . " points to the server <b>\n" . $d['target'] . "</b>\n whose IP address is <b>\n" . $server['ip'] . "</b>. It has a priority of <b>\n" . $d['pri'] . "</b>\n.";
}
if ( $d['target'] == $domain1 ) {
echo "<div id='mx-status'>There is an issue with this MX Record</div>\n";
} else {
echo "<div id='mx-status'>This MX Record looks fine.</div>\n";
}
break;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否考虑过使用
getmxrr()
来获取域的 mx 记录?此处的文档: https://www.php.net/manual/en/function .getmxrr.phpHave you considered using
getmxrr()
to get the mx records for the domain? Documentation here: https://www.php.net/manual/en/function.getmxrr.php