To protect a field where users can submit feedback on a sort of forum-like page, I use the API of stopforumspam.com to compare the IP of the visitors against the stopforumspam blacklist.
However, sometimes, stopforumspam is down, for maintenance of because spammers are DDOSing the domain. And that makes the loading of that page take almost forever.
My current code uses the
try { }
catch(Exception $e) { }
method.
In full detail :
$visitorip=getip();
try
{
// using code from http://guildwarsholland.nl/phphulp/testspambot.php to try and block spammers
$xml_string = file_get_contents('http://www.stopforumspam.com/api?ip='.$visitorip);
$xml = new SimpleXMLElement($xml_string);
if($xml->appears == 'yes'){
$spambot = true;
file_put_contents("list.txt" , date('c').", ".$visitorip."\n", FILE_APPEND);
$spambot_info = $ip.',';
die("I'm sorry but there has been an error with the page, please try again tomorrow or contact the admin if your report can't wait, thank you!");
}
}
catch(Exception $e)
{
echo 'Error connecting to the main antispam checking database, please send an email to the admin through the main contact page, if that problem lasts for more than a pair of hours, THANK YOU !! <br>Here is the complete error message to report : <br>' .$e->getMessage();
}
Where this is imperfect: when stopforumspam is down, there will be a full 45-seconds loading time with a blank page, before the catch() error message shows up and my page finally loads. Server wait time, max php execution time, or standard wait delay, most likely.
Would you know how I can shorten the time during which my script attempts to connect (before throwing an error) to max 10 seconds ? Much appreciated!