2012-11-15 9 views
6
$ldaphost = "my_host_name"; 
$ds=ldap_connect($ldaphost) or die("Could not connect to $ldaphost"); 
ldap_set_option ($ds, LDAP_OPT_REFERRALS, 0); 
ldap_set_option ($ds, LDAP_OPT_PROTOCOL_VERSION, 3); 

if ($ds) 
{ 
    $basedn = 'my_dc_string'; 
    $samaccountname = 'my_user_name'; 
    $filters = "(samaccountname={$samaccountname})"; 
    $result = ldap_search($ds, $basedn, $filters); 
} 

Come si ottiene l'elenco di tutti gli utenti da LDAP utilizzando PHP? Il codice di cui sopra non sulla funzione ldap_search dare questo avvertimentoCome ottenere l'elenco degli utenti dalla directory LDAP usando PHP?

"Attenzione: ldap_search(): Ricerca: errore di gestione"

il mio nome utente, hostldap ecc sono corrette. Comunque non sono sicuro dei filtri.

risposta

14
/** 
* Get a list of users from Active Directory. 
*/ 
$ldap_password = 'PASSWORD'; 
$ldap_username = '[email protected]'; 
$ldap_connection = ldap_connect(HOSTNAME); 
if (FALSE === $ldap_connection){ 
    // Uh-oh, something is wrong... 
} 

// We have to set this option for the version of Active Directory we are using. 
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version'); 
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search. 

if (TRUE === ldap_bind($ldap_connection, $ldap_username, $ldap_password)){ 
    $ldap_base_dn = 'DC=XXXX,DC=XXXX'; 
    $search_filter = '(&(objectCategory=person)(samaccountname=*))'; 
    $attributes = array(); 
    $attributes[] = 'givenname'; 
    $attributes[] = 'mail'; 
    $attributes[] = 'samaccountname'; 
    $attributes[] = 'sn'; 
    $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes); 
    if (FALSE !== $result){ 
     $entries = ldap_get_entries($ldap_connection, $result); 
     for ($x=0; $x<$entries['count']; $x++){ 
      if (!empty($entries[$x]['givenname'][0]) && 
       !empty($entries[$x]['mail'][0]) && 
       !empty($entries[$x]['samaccountname'][0]) && 
       !empty($entries[$x]['sn'][0]) && 
       'Shop' !== $entries[$x]['sn'][0] && 
       'Account' !== $entries[$x]['sn'][0]){ 
       $ad_users[strtoupper(trim($entries[$x]['samaccountname'][0]))] = array('email' => strtolower(trim($entries[$x]['mail'][0])),'first_name' => trim($entries[$x]['givenname'][0]),'last_name' => trim($entries[$x]['sn'][0])); 
      } 
     } 
    } 
    ldap_unbind($ldap_connection); // Clean up after ourselves. 
} 

$message .= "Retrieved ". count($ad_users) ." Active Directory users\n"; 
+0

Questo funziona ... tranne il set di risultati è vuoto, cercando in esso. Grazie –

+0

Fantastico. Ho funzionato. Ora via per ottenere tutte le informazioni per tutti gli utenti –

+0

campione eccellente, grazie. – Dax

1

Da php.net:

ldap_connect() // establish connection to server 
    | 
ldap_bind()  // anonymous or authenticated "login" 
    | 
do something like search or update the directory 
and display the results 
    | 
ldap_close()  // "logout" 

Si dovrebbe usare ldap_bind() il login & dopo tutto operartion (s) ldap_close() per chiudere la connessione.

Example usage here.

Problemi correlati