Jump to content

PHP, LDAP, List OUs and sub-OUs


AndieB

Recommended Posts

Hi all!

 

I am in need of help and hope that some kind soul out there is able to provide some support.

 

I have managed to connect to Microsoft Active Directory, using PHP and LDAP. Also managed to performe a search in AD for UserAccount information.

 

What I need help with, is to search in AD within an OU (Organizational Unit) and list the reslut into an own array.

 

i.e.

In AD the structure looks like this:

 

+Sales

-USA

-Sweden

-Belgium

+Finance

-Usa

-Sweden

-Belgium

 

I would like to have them in an array in the format:

Sales/USA

Sales/Sweden

Sales/Belgium

Finance/Usa

Finance/Sweden

Finance/Belgium

 

I would very much appreciate some help!

 

Sincerely,

Andreas

Link to comment
https://forums.phpfreaks.com/topic/51620-php-ldap-list-ous-and-sub-ous/
Share on other sites

Once you query LDAP it returns basically a large multidimensional array that you can then parse through and use the data however you like...put in a new array or what-not...

 

//connect to your ldap server

$dn = "ou=sales, DC=USA"; //use your base DN here
$filter = "(|(sAMAccountname=*))";

$search_result = ldap_search($ldap_connection, $db, $filter) or die(ldap_error($ldap_connection));
$result = ldap_get_entries($ldap_connection, $search_result);

for ($i = 0; $i < $info['count']; $i++) {
  echo '<pre>' . print_r($info[$i], true) . '</pre><br />';

  //if you wanted to know, for example, what groups they are a member of...
  echo '<pre>' . print_r($info[$i]['memberof'], true) . '</pre>';

}

  • 3 weeks later...

Thank you so much for your feedback!

 

Follow-up question:  ;)

Why do you use sAMAccoutname as filter? Does it work better that ObjectClass=*?

 

 

Again, thank you very much for your time and effort!

 

Sincerely,

Andreas

 

 

Once you query LDAP it returns basically a large multidimensional array that you can then parse through and use the data however you like...put in a new array or what-not...

 

//connect to your ldap server

$dn = "ou=sales, DC=USA"; //use your base DN here
$filter = "(|(sAMAccountname=*))";

$search_result = ldap_search($ldap_connection, $db, $filter) or die(ldap_error($ldap_connection));
$result = ldap_get_entries($ldap_connection, $search_result);

for ($i = 0; $i < $info['count']; $i++) {
  echo '<pre>' . print_r($info[$i], true) . '</pre><br />';

  //if you wanted to know, for example, what groups they are a member of...
  echo '<pre>' . print_r($info[$i]['memberof'], true) . '</pre>';

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.