2015-12-21 156 views
0

我試圖從一個活動目錄中獲取所有用戶的全名,我只管理了一個用戶,就好像我想要執行登錄功能一樣,但是當我試圖將我的代碼概括給所有用戶時,我無法使其工作。如何使用PHP從LDAP目錄中檢索所有用戶?

這裏是我的代碼:

<?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; 
?> 

我是新在此我

回答

0

只需卸下標準,從您的過濾特定用戶:

$filter = "(objectCategory=person)";