Posted: 10th December 2013

Author: Ric

Tagged: Tutorials

checkdnsrr() PHP Function for Windows IIS servers

When handling form submissions that ask for a visitors email address, such as a registration form or mailing list sign-up, it's a good idea to verify those addresses. At the very least, you should check that the entered text is in the correct format for an email address, usually via regular expressions. Ideally, you should also check that the domain part of the address is a real mail server. To do this you can use the checkdnsrr() function of PHP. If the function returns a valid MX record, you can be sure that there's a real mail server on that domain.

Unfortunately IIS servers didn't have a checkdnsrr() function until PHP version 5.3 and many administrators haven't yet upgraded from the 5.2 series. Fear not, you can copy + paste the below to replicate this function!

if(!function_exists('checkdnsrr')) {
    function checkdnsrr($host, $record = 'MX') {
        if(!empty($host) && !empty($record)) {
            exec("nslookup -type=$record $host", $res);
            foreach ($res as $val) {
                if(stristr($val, $host))
                    return true;
            }
        }

        return false;
    }
}

Blog posts written by former QWeb employees are not necessarily an accurate indication of the current opinions of QWeb Ltd and the information provided in tutorials might be biased or subjective, or might become out of date.

Discuss this post

Mary Grant
11/12/2013 08:02

Should that go on server here as part of picturepostbox as I get loads of spam (about 500 week). Would that stop the spoof e-mail addresses?

Ric
11/12/2013 09:40

It would definitely reduce the number of fake sign-ups but probably not general spam, unless it’s all going through the on-site contact form.

Leave a comment

Your email address is used to notify you of new comments to this thread, and also to pull your Gravatar image. Your name, email address, and message are stored as encrypted text and you won't be added to any mailing list and your details won't be shared with any third party.