More Than Just Web Design | INTERNET ENGINEERING | APPLICATION | DESIGN

Holy Host, Batman

Posted: 20/05/14

PHP's gethostbyaddr() function fails for no apparent reason.

There are seemingly millions of "SEO gurus" on the Internet at large, and the Indian subcontinent has its fair share. It comes as little surprise that we're frequently contacted by what seems like all of them, peddling their services for a few dollars.

After a while, the incessant use of the contact form gets rather tiring. I figured the way forward was to override the Concrete5 form controller's action_submit_form() method, and, if the IP address resolves back to somewhere in India, pretend that the CAPTCHA code was incorrect.

This was a cunning plan, but unfortunately the wheels came off because the PHP function gethostbyaddr() always returned the IP address, not the host name. This happened on all IP addresses, even ones that the "host", "nslookup" or "dig" tools would return a value host name for. Hmmm....

Various amounts of Googling revealed no solution, but I came across the dns_get_records() function, which did the trick:

$h=implode(".",array_reverse(explode(".",$_SERVER['REMOTE_ADDR']))).'.in-addr.arpa';
$records=dns_get_record($h);
foreach ($records as $hostparts) {
    $array=explode('.',$hostparts['target']);
    $tld=array_pop($array);
    if ($tld=='in') // anything from India is rejected
        $errors['india']="Incorrect captcha code!";
 }

Silence is golden.