Search code examples
phpldapfsockopen

PHP fsockopen ldap domain search


Context:
I am using a domain name address to try and check the connections to a domain controller but not all domain controllers are accessible from the server running the PHP. So I am using a loop to see if a domain controller is there to respond with using fsockopen.

Domain.ad points to 15 possible domain controllers. server.domain.ad, server2.domain.ad, etc

...
$i = 0;
do
{
    $fp = fsockopen("domain.ad", 389, $errno, $errstr, 1);
    $i++;
}
while(!$fp && $i < 15);
...
//$ip = $fp->getAddress(); or something like that.

Question:
Is there a way to find out from the fsockopen connection of what IP address / domain controller it was successful to connect with to be used later in the code to complete the LDAP bind with? Or is there a better way to do this without using 1 domain controller?


Solution

  • My Solution for iterating through the domain controllers.

    $ipDomain = dns_get_record("domain.ad", DNS_A);
    $i = 0;
    do
    {
        $fp = fsockopen($ipDomain[$i]['ip'], 389, $errno, $errstr, 1);
        if(!$fp)
        {
            $i++;
            if($i >= sizeof($ipDomain))
            {
                break;
                //die();
            }
        }
    }
    while(!$fp);
    ...
    ldap_connect($ipDomain[$i]['ip']);
    ...