Wednesday, November 1. 2006
Posted by Jonathan Street
in Website Management, PHP Programming at
17:35
Comments (0)
Trackbacks (2)
Comments (0)
Trackbacks (2)
Automatically checking the black lists
This is just a short followup to the post I made a short while ago looking at checking to see whether the server you host your site on has been blacklisted for sending spam. Although checking your status is important it is also rather boring. Instead of manually checking every so often we can create a script which is automatically called and which contacts us when there is a problem.
I now offer a service on this site which allows you to track your spam status.
In my last post I gave a link to a service which pools together the results from many different rbl (real-time blacklist) services. The easiest approach to creating this script would be to lookup the page and then use a regular expression to see if any rows are highlighted red. The problem with this approach is that the service appears to be financed by displaying google adsense adverts. It is not fair to use the providers bandwidth without giving anything back (page views which could lead to clicks) so we need an alternative approach.
In this instance I am going to use a PEAR package. The PEAR package is called Net_DNSBL so if you don’t have it installed on your system you’re going to need it. There are a few dependencies so if you don’t have the following packages I suggest you install them in the order below
Net_URL
Net_Socket
HTTP_Request
Net_CheckIP
Cache_Lite
. . . and finally Net_DNSBL
Once we have the packages in place we have a few decisions to make. How are we going to get the ip number for our server? Which rbl services are we going to query?
There are two methods of obtaining the ip number for the server that I am going to consider. The first is to get the ip address from the cpanel interface and then ‘hard-code’ the value into the script. The second way is to use the value stored in the ‘$_SERVER['REMOTE_ADDR']’ variable. The first method will be best if you plan on manually accessing the page. The second method only really works when using the script with cron. The cron daemon is on the webserver so when it accesses the script the '$_SERVER['REMOTE_ADDR']' variable will actually be the address of the server. If you are in any doubt it is probably best to manually enter the ip number for your server.
The second question that needs to be addressed is which rbl services to query. I suggest you go back to the multi-check service discussed in my previous article and choose a few from the list there. I'm going to use the following
dnsbl.sorbs.net
sbl-xbl.spamhaus.org
bl.spamcop.net
Time for some code . . .
<?php
require_once 'Net/DNSBL.php';
$dnsbl = new Net_DNSBL();
$remoteIP = $_SERVER['REMOTE_ADDR'];
$dnsbl->setBlacklists(array('sbl-xbl.spamhaus.org', 'dnsbl.sorbs.net', 'bl.spamcop.net'));
if ($dnsbl->isListed($remoteIP)) {
// We're in a black list!
//Send an email to ourselves to alert us.
mail("you@yourdomain.com", "We're listed in an rbl.", "Your monthly check of the black lists has found that your IP address is listed. It might be time to look at changing hosts.");
echo "Listed!";
}
else {
echo "Not listed.";
//No need to do anything
//All is well
}
?>
require_once 'Net/DNSBL.php';
$dnsbl = new Net_DNSBL();
$remoteIP = $_SERVER['REMOTE_ADDR'];
$dnsbl->setBlacklists(array('sbl-xbl.spamhaus.org', 'dnsbl.sorbs.net', 'bl.spamcop.net'));
if ($dnsbl->isListed($remoteIP)) {
// We're in a black list!
//Send an email to ourselves to alert us.
mail("you@yourdomain.com", "We're listed in an rbl.", "Your monthly check of the black lists has found that your IP address is listed. It might be time to look at changing hosts.");
echo "Listed!";
}
else {
echo "Not listed.";
//No need to do anything
//All is well
}
?>
This is actually quite simple. The PEAR class does all the hard work for us so all we need to do is choose which services we want to check, supply our IP address and fetch the result. I've used a simple if statement to handle the output. If our IP address is listed an email is sent to alert us. If our IP address isn't listed then all is well and the script does nothing.
With the script loaded on to our server we can set up a cron job to run the script each month. It will look something like this
0 3 1 lynx -dump http://www.yourdomain.com/cron/cron_rbl.php
This runs the script at 3am on the first day of every month.
With that set up we can sit back and relax safe in the knowledge that if a problem does crop up we'll find out about it.

The package used a little while ago in an article explaining how to check the spam blacklists has recently been updated. Net_DNSBL 1.2.1 is now available. It looks like there haven't been any big changes but if you want the latest and greatest feel free
Tracked: Jan 01, 15:23
For a couple of weeks now I've been intending to release two new tools for this site. For one week they've been live on the site (though not linked to) and still I haven't mentioned them. It could be said I'm procrastinating. This is a shame because
Tracked: Jun 20, 21:27