Zend - The PHP Company




External

Add Code


checkdnsrr and getmxrr for windows NT  

Type: code fragment
Added by: twilop
Entered: 28/11/2000
Last modified: 01/12/2000
Rating: - (fewer than 3 votes)
Views: 6700
checkdnsrr and getmxrr do not work on windows NT making resolution of email address a pain in the bum. Use these functions instead and you'll get the same functionality.


<?php
/******************************************************

These functions can be used on WindowsNT to replace
their built-in counterparts that do not work as
expected.

checkdnsrr_winNT() works just the same, returning true
or false

getmxrr_winNT() returns true or false and provides a
list of MX hosts in order of preference.

*******************************************************/

function checkdnsrr_winNT$host$type '' )
{

    if( !empty( 
$host ) )
    {
        
        
# Set Default Type:
        
if( $type == '' $type "MX";

        @
exec"nslookup -type=$type $host"$output );

        while( list( 
$k$line ) = each$output ) )
        {
            
            
# Valid records begin with host name:
            
if( eregi"^$host"$line ) )
            {
                
# record found:
                
return true;
            }

        }

        return 
false;

    }

}

function 
getmxrr_winNT$hostname, &$mxhosts )
{

    if( !
is_array$mxhosts ) ) $mxhosts = array();

    if( !empty( 
$hostname ) )
    {

        @
exec"nslookup -type=MX $hostname"$output$ret );

        while( list( 
$k$line ) = each$output ) )
        {
            
            
# Valid records begin with hostname:
            
if( ereg"^$hostnametMX preference = ([0-9]+), mail exchanger = (.*)$"$line$parts ) )
            {

                
$mxhosts$parts[1] ] = $parts[2];

            }

        }

        if( 
count$mxhosts ) )
        {

            
reset$mxhosts );

            
ksort$mxhosts );

            
$i 0;

            while( list( 
$pref$host ) = each$mxhosts ) )
            {
                
$mxhosts2[$i] = $host;
                
$i++;
            }

            
$mxhosts $mxhosts2;

            return 
true;

        }
        else
        {

            return 
false;

        }

    }

}

?>


Usage Example


<?php

if ( getmxrr_winNT"dataactive.co.uk"$hosts ) )
{
echo 
$hosts[0];
}

# Prints: mail.mmsc.co.uk

?>


Rate This Script





Search



This Category All Categories