Friday May 09, 2008

Domain Expiration Notify Script

Introduction
If you're in the web hosting business, a server admin, or heck.. just have too many domains at more registrars than you can shake a stick at, you might be worried that eventually you're going to miss re-registering an expiring domain. Most registrars will send an email or two when your domain's about to expire, but they don't go overboard to notify you. If you're not listed as a domain contact or your email address changes, you need another way to make sure the domains you're managing don't expire without giving you proper notification. This script will should help keep your fears at ease and hopefully give you proper notification of that impending doom that is domain expiration =)

Creating the Database
First up is to create the database table for the domain list. This isn't a very sophisticated setup -- there's no backend to manage things, so you will have to be good and aquainted with PHPMyAdmin.

CREATE TABLE `domains` (
`domainName` varchar(100) NOT NULL default '',
`registrar` varchar(100) NOT NULL default '',
`expiration` date NOT NULL default '0000-00-00',
`status` varchar(100) NOT NULL default '',
`lastChecked` timestamp(14) NOT NULL
) TYPE=MyISAM;

Example of what to enter in this database table for each domain you want to check:
domainName - "google.com"
registrar - "GoDaddy"
expiration - Leave Blank
status - Leave Blank
lastChecked - Leave Blank

Without Further Adew, Script #1
What's that? Script #1? Yeah.. there's actually two scripts that are needed for this particular application. The first script runs in CRON and grabs WHOIS information about a domain. I have CRON setup to run the script once every half hour, and it only does a WHOIS lookup on one domain each run. Most WHOIS servers limit the number of times you can connect within a certain time interval, and since you really only need to check domain status once a day I figured once every half hour was good enough to check through my domain list. You can of course experiment and set this a little shorter, but I wouldn't run it more than once every 15 minutes if they aren't your WHOIS servers.

<?
/*************************************************************
 * Whois Domain Check
 * File Name: whois.php
 *------------------------------------------------------------
 * Author: Wayne Eggert
 * Email: wayne@techdose.com
 * 
 * DESCRIPTION: Grabs domain information from a WHOIS server
 * and inserts into a database.
 *************************************************************/
include("database.php");

// Select one domain from the database that hasn't been checked yet
$sql "SELECT domainName from domains WHERE 1 ORDER BY lastChecked ASC";
$result mysql_query($sql);
$row mysql_fetch_row($result);
$domain $row[0];

if(
mysql_num_rows($result) == 0){
    die(
"No domains found in the database.");
}

// Grab the WHOIS information for the domain selected
// ---------------------------------------------------------------
$domainExt substr($domain, -3); // grab the domain extension

if(strtoupper($domainExt) == 'ORG'){
    
$whoisServer 'whois.melbourneit.com';
}else{
    
$whoisServer 'rs.internic.net';
}

// Run the whois lookup on your server
exec('whois -h '.$whoisServer.' '.$domain,$whois,$error);
foreach(
$whois as $whoisline){
    if(
strstr($whoisline,"Expiration")){
        
$whoisline str_replace("Expiration Date:","",$whoisline);
        
$whoisline trim($whoisline);
        
$expiration substr($whoisline,0,11);
    }

    if(
strstr($whoisline,"Status")){
        
$statusline $whoisline;
    }
}

$status str_replace("Status:","",$statusline);
$status trim($status);
// ---------------------------------------------------------------

// Parse out expiration information
$expirationArr explode("-",$expiration);
$expiration_day $expirationArr[0];
$expiration_month $expirationArr[1];
$expiration_year $expirationArr[2];

$expiration_month strtolower($expiration_month);
switch(
$expiration_month){
    case 
'jan':
        
$expiration_month='01';
    break;
    case 
'feb':
        
$expiration_month='02';
    break;
    case 
'mar':
        
$expiration_month='03';
    break;
    case 
'apr':
        
$expiration_month='04';
    break;
    case 
'may':
        
$expiration_month='05';
    break;
    case 
'jun':
        
$expiration_month='06';
    break;
    case 
'jul':
        
$expiration_month='07';
    break;
    case 
'aug':
        
$expiration_month='08';
    break;
    case 
'sep':
        
$expiration_month='09';
    break;
    case 
'oct':
        
$expiration_month='10';
    break;
    case 
'nov':
        
$expiration_month='11';
    break;
    case 
'dec':
        
$expiration_month='12';
    break;
}

$expiration $expiration_year."-".$expiration_month."-".$expiration_day;

// Update domain information in database
$sql "UPDATE domains SET expiration='".$expiration."',status='".$status."',lastchecked=NOW() WHERE domainName='".$domain."'";
mysql_query($sql);
?>

Explanation:

  • The first line is just a database include I created. Replace this with your include file or MySQL connection code
  • Next we just pull the next domain from the database that hasn't been checked, according to the lastChecked timestamp
  • WHOIS servers are set according to the domain extension. I've found that .org domains do not return the correct WHOIS results from Internic's WHOIS server. Any other exceptions or additional servers could be added here. If you wanted to run more domains through the script you could add code to pick a random WHOIS server.
  • The WHOIS lookup is performed & information on the domain expiration & current domain status is retrieved.
  • Expiration is formatted to be entered into the database
  • Domain information is updated in the database table




Comments:
How to set cron tab
Posted 10/16/06 12:01AM by creativetest
How to set cron tab
how to get the registrar
Posted 06/03/07 3:40PM by rocko
Hi, I see your script. It is really good and help me a lot.
But Im trying to use a registrar field. I use different string functions but I couldn't.
There are any way to use the registrar field?
Thanks in advance.



If you have an account, please login below. You can sign up for a new account, it's free!
You can also post anonymously as Anonymous Techdoser if you leave the username/password fields blank.

*Username
*Password
Subject
Comment  
 


*Due to URL spamming, comments will be approved before being posted.




Partners

Comparison shopping:
GPS, Digital camera,
LCD TV, Nintendo Wii,
iPod, Tomtom,
Asus, Digital Photo Frame
www.shopbot.com.au

Sony LCD TV
Read Reviews On Sony LCD TV's


Advertising Sponsors