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

}

Link to comment
Share on other sites

  • 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>';

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.