简体   繁体   中英

php ldap_search not returning results

I'm establishing a connection to our Active Directory listing of users/employees. I've done this through .NET, but cant get it to work in my PHP app.

I consistantly get a count of 0.

I've tried using samaccountname and sAMaccountname as filters, this does not change the result.

I am successfully connecting, as changing the $ldap will no longer find the server.

I am using valid credentials, as changing $authUser or $authPath provide an authorized error message.

The ldap_bind (i presume) is working, because it does perform the search and outputs a count of 0.

Here is my code:

<?php 
try{
    $ldap = "vmc-dc.CompanyName.vmc";
    $authUser = "vmc\\MyUsername";
    $authPass = "MyPassword";
    $baseDn = "dc=vmc-dc,dc=CompanyName,dc=com";
    $filter="(&(objectClass=user)(samaccountname=*))";

    $conn = ldap_connect($ldap, 389) ;

    if ($conn) {

        ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_set_option($conn, LDAP_OPT_REFERRALS, 0);

        // binding to ldap server
        $ldapbind = ldap_bind($conn, $authUser, $authPass);

        // verify binding
        if ($ldapbind) {
            //$sr=ldap_read($conn, $baseDn, $filter);
            $sr=ldap_search($conn, $baseDn, $filter);  

            $number_returned = ldap_count_entries($conn,$sr);
            echo "Count: " . $number_returned . "<br/>";

            $entry = ldap_get_entries($conn, $sr);
            ldap_close($conn);
            echo "value = '" . $entry[0] . "'";

        } else {
            echo "LDAP conn ok...";
        }
    }
} catch (Exception $e) {
}
?>

I wonder if your filter is too broad, all user class objects (which includes computers, to Brian Desmond's point) and is returning more than 1000 found objects. In which case AD will error, and return nothing. I would expect you would get a returned error, so this may not be likely. But a more constrained filter, and/or a repetition with a standalone LDAP tool could help validate this idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM