如何使用 PHP 从 LDAP 目录中检索所有用户?

How to retrive all users from LDAP directory using PHP?

我正在尝试从活动目录中获取所有用户的全名,我设法只获取了一个用户,就好像我想执行登录功能一样,但是当我试图将我的代码推广到所有用户时,我无法做到它有效。

这是我的代码:

<?php
// active directory 
$ldap_host    = "xxx";
$ldap_port    = "xxx";
// Active Directory DN
$ldap_dn[]    = "ou=xxx,DC=xxx,DC=xxx,DC=xx";
$ldap_dn[]    = "ou=xxx,DC=xxx,DC=xxx,DC=xx";
// Domain, for purposes of constructing $user
$ldap_usr_dom = "@xxx.xxx.xx";
// connect to active directory
$ldap         = ldap_connect($ldap_host, $ldap_port);
$ldap_id[]    = $ldap;
$ldap_id[]    = $ldap;
$username     = "xxx";
$password     = "xxx";
// verify user and password
if ($bind = @ldap_bind($ldap, $username . $ldap_usr_dom, $password)) {
    $filter = "(&(objectCategory=person)(sAMAccountName=" . $username . "))";
    $result = ldap_search($ldap_id, $ldap_dn, $filter) or exit("Unable to search LDAP server");
    foreach ($result as $value) {
        if (ldap_count_entries($ldap, $value) > 0) {
            $search = $value;
            break;
        }
    }

    if ($search) {
        $entries = ldap_get_entries($ldap, $search);
        for ($x = 0; $x < $entries['count']; $x++) {
            if (!empty($entries[$x]['cn'][0])) {
                $ad_users[] = array(
                    'fullname' => trim($entries[$x]['cn'][0])
                );
                print_r($ad_users);
                //print_r($entries);
            }
        }
    }
    ldap_unbind($ldap); // Clean up after ourselves.
}

$message .= "Retrieved " . count($ad_users) . " Active Directory users\n";
echo $message;
?>

我是新手

只需从您的过滤器中删除特定用户的条件:

$filter = "(objectCategory=person)";