How to Check the Domain Expiry Date Using PHP: gaurangdangi.com
A domain name is one of the most valuable assets for any online presence. Failing to renew it on time can result in website downtime or, worse, losing ownership. Automating domain expiration checks can help avoid such scenarios.
In this blog, we’ll guide you through a PHP script to fetch and monitor domain expiration dates and notify you via email if a domain is about to expire.
Step-by-Step Guide
What Does the Script Do?
This PHP script:
- Fetches the expiration date of a domain using a WHOIS API.
- Notifies via email if the domain is nearing expiration.
- Logs the expiration status of each domain.
Code Breakdown
Below is a detailed explanation of the code:
1.Configuration
$notificationDays = 7; // Notify before these many days
$notificationEmail = "developer@gaurangdangi.com"; // Recipient email
$domains = ["gaurangdangi.com"]; // List of domains
These variables define the notification threshold in days, the recipient email for alerts, and the list of domains to monitor.
2.Fetch Domain Expiration Date
function getDomainExpirationDate($domain) {
$url = "https://api.whois.vu/?q=$domain"; // WHOIS API endpoint
$response = file_get_contents($url);
$data = json_decode($response, true);
if (isset($data['expires'])) {
return $data['expires']; // Expiration date as UNIX timestamp
}
return false;
}
- This function sends a request to a free WHOIS API service and retrieves the domain’s expiration date.
- Note: Replace the example WHOIS API (
https://api.whois.vu
) with your preferred API for production usage.
3.Send Notification
function sendNotification($subject, $message, $to) {
$headers = "From:info@gaurangdangi.com"; // Replace with a valid email
$mail_sent = mail($to, $subject, $message, $headers);
if ($mail_sent) {
echo "<br>Email sent successfully!";
} else {
echo "<br>Failed to send email.";
}
}
The sendNotification
function uses PHP’s mail
function to send an alert when a domain is close to expiration.
4.Check Domains and Notify
foreach ($domains as $domain) {
$expirationDate = getDomainExpirationDate($domain);
if ($expirationDate) {
$daysLeft = ($expirationDate - time()) / 86400;
$daysLeft = (int)$daysLeft;
if ($daysLeft <= $notificationDays) {
$subject = "Domain Expiry Notification: {$domain}";
$message = "The domain <b>'{$domain}'</b> is expiring on " . date("d-m-Y", $expirationDate) . ". Please renew it soon.";
sendNotification($subject, $message, $notificationEmail);
} else {
echo "Domain <b>'{$domain}'</b> is valid. Expiration date: " . date("d-m-Y", $expirationDate) . "<br>";
}
} else {
echo "<br>Couldn't fetch expiration date for $domain.\n";
}
The script loops through each domain, checks its expiration date, and sends a notification email if the domain expires within the notification threshold.1
- The script loops through each domain, checks its expiration date, and sends a notification email if the domain expires within the notification threshold.
↩︎
Benefits of Automating Domain Expiry Checks
- Avoid Inconvenience: Proactively renew domains before expiry.
- Cost-Effective: Prevent the risk of losing your domain and having to pay a premium to recover it.
- Time-Saving: Automates a crucial but tedious task, ensuring consistency.
Customizations
- Logging: Add logging using
error_log
or store results in a database. - Advanced Email Handling: Use a library like PHPMailer for better email management.
- Custom WHOIS API: Use a paid, reliable API for production.
Conclusion
This script provides a simple yet effective way to monitor domain expiration dates. With a few customizations, you can integrate it into your monitoring system and receive timely reminders, ensuring your domains stay active without interruptions.