Friday April 19, 2024

Domain Expiration Notify Script

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
page1 page2 page3 page4


Comments:
no error but expiry date is not coming in database
Posted 03/19/11 4:26AM by leaftemp
no error all done, but no expiry date is coming in database i think this is because of wrong date format as on domain table default 0000-00-00 is different from the date format in whois server.

So please help me to correct my code.
Re: Expiration
Posted 09/21/09 6:17PM by AceBHound
Sounds like the $expiration string isn't getting built correctly. Before the SQL call to UPDATE the domains table, try echo'ing out the $expiration variable and see if it contains YEAR-MONTH-DAY (like 2009-12-31) or if it looks like "--". It's possible the WHOIS server you're using could be using a slightly different format and you'll need to adjust the code that parses out data from the WHOIS call.
Expiration
Posted 09/15/09 4:52PM by Anonymous Techdoser
Great script, everything seems to work well except the expiration field in my database does not update.
Thanks!
Posted 08/14/09 12:21PM by thomascim
Hi Wayne, thanks very much for this useful code.

One suggestion is maybe adding an extra field to the database table for the extension.

This way you can also check domain extensions that have more or less than 3 characters like co.uk or .de, .eu, etc.

Thanks again,

Thomas

problem with exec()
Posted 02/06/09 4:28PM by jschrader
I have installed the code, and am getting an error about the exec(), to be specific:


Warning: exec() has been disabled for security reasons in /home/a5704353/public_html/notify.php on line 38

Free Web Hosting

PHP Error Message

Warning: Invalid argument supplied for foreach() in /home/a5704353/public_html/notify.php on line 39


what is going on?
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.
How to set cron tab
Posted 10/16/06 12:01AM by creativetest
How to set cron tab