PHP checkdnsrr Function
last modified April 4, 2025
The PHP checkdnsrr
function checks DNS records for a given host.
It's useful for validating domain names and email addresses.
Basic Definition
checkdnsrr
checks if DNS records exist for a specified host.
It can verify different record types like MX, A, CNAME, etc.
Syntax: checkdnsrr(string $host, string $type = "MX"): bool
.
Returns true if records exist, false otherwise. Requires DNS server access.
Basic Email Domain Validation
This example checks if a domain has MX records for email validation.
<?php declare(strict_types=1); $domain = "example.com"; if (checkdnsrr($domain, "MX")) { echo "Domain $domain has valid MX records"; } else { echo "Domain $domain has no MX records"; }
This checks if the domain can receive email by verifying MX records. MX records are essential for email delivery between servers.
Checking A Records
This demonstrates checking A records to verify a domain's IP resolution.
<?php declare(strict_types=1); $host = "www.example.com"; if (checkdnsrr($host, "A")) { echo "$host resolves to an IP address"; } else { echo "$host doesn't resolve to an IP"; }
A records map hostnames to IP addresses. This check confirms the domain has proper DNS configuration for web hosting.
Checking Multiple Record Types
This example checks multiple DNS record types for comprehensive validation.
<?php declare(strict_types=1); $domain = "example.com"; $checks = ["A", "MX", "TXT"]; foreach ($checks as $type) { $result = checkdnsrr($domain, $type) ? "exists" : "missing"; echo "$type record: $result\n"; }
This provides a complete DNS health check by verifying multiple record types. Different record types serve different purposes in domain configuration.
Email Validation Function
This shows a complete email validation function using checkdnsrr
.
<?php declare(strict_types=1); function validateEmailDomain($email) { $parts = explode('@', $email); if (count($parts) != 2) return false; $domain = $parts[1]; return checkdnsrr($domain, "MX"); } $email = "user@example.com"; echo validateEmailDomain($email) ? "Valid" : "Invalid";
This function splits the email to extract the domain, then checks for MX records. This is more reliable than simple format validation.
Checking CNAME Records
This example verifies if a hostname has CNAME (alias) records configured.
<?php declare(strict_types=1); $host = "blog.example.com"; if (checkdnsrr($host, "CNAME")) { echo "$host is an alias (CNAME)"; } else { echo "$host is not a CNAME record"; }
CNAME records create aliases pointing to other domain names. This check helps identify if a hostname is configured as an alias.
Best Practices
- Caching: Cache results to avoid repeated DNS queries
- Error Handling: Handle cases where DNS queries fail
- Performance: Consider timeout values for slow responses
- Validation: Combine with other validation methods
Source
This tutorial covered the PHP checkdnsrr
function with practical
examples for DNS record validation in various scenarios.