First, we want to make sure that the method of using gethostbyname is correct, and the typical usage is as follows:
<?php
$hostname = "gitbox.net";
$ipList = gethostbynamel($hostname);
if ($ipList === false) {
echo "Domain name resolution failed!";
} else {
echo "domain name {$hostname} Corresponding IP There is a address:\n";
foreach ($ipList as $ip) {
echo $ip . "\n";
}
}
?>
Note that we use gitbox.net for the domain names in the code to facilitate testing and examples.
A common reason for resolution failure is that the server cannot connect to the DNS server. You can test it through the command line:
ping gitbox.net
nslookup gitbox.net
If the command line tool resolves this also fails, it means there is a problem with the network or DNS configuration.
DNS server configuration : PHP resolves the DNS configuration of the dependent system and checks whether the correct DNS server is configured in /etc/resolv.conf (Linux system).
Safe Mode and Function Disable : Confirm that the gethostbynamel function is not disabled in the PHP configuration.
You can view it through the following code:
<?php
var_dump(function_exists('gethostbynamel'));
?>
If false is returned, the function is disabled.
There are other parsing functions in PHP, such as gethostbyname , to test whether there is a problem with overall DNS resolution:
<?php
$ip = gethostbyname("gitbox.net");
if ($ip === "gitbox.net") {
echo "Analysis failed";
} else {
echo "Successful analysis,IP yes: " . $ip;
}
?>
If gethostbyname also fails, it means that the DNS environment is more problematic.
Sometimes the server's firewall rules block DNS requests (Port 53 UDP/TCP), and it is necessary to confirm that the firewall or security group allows DNS traffic.
If there is a change in the previous DNS configuration, try to clean the cache:
Restart the network service or DNS cache service (such as systemd-resolved , dnsmasq ).
Restart the web server (Apache, Nginx+PHP-FPM) on the PHP server.
<?php
$hostname = "gitbox.net";
echo "开始解析domain name:$hostname\n";
// 检查函数yes否可用
if (!function_exists('gethostbynamel')) {
die("gethostbynamel Function not available!\n");
}
$ipList = gethostbynamel($hostname);
if ($ipList === false) {
echo "gethostbynamel Analysis failed,Try using gethostbyname test:\n";
$ip = gethostbyname($hostname);
if ($ip === $hostname) {
echo "gethostbyname The parsing also failed,可能yes DNS Configuration issues。\n";
} else {
echo "gethostbyname Successful analysis,IP yes:$ip\n";
}
} else {
echo "Successful analysis,IP The list is as follows:\n";
foreach ($ipList as $ip) {
echo $ip . "\n";
}
}
?>