Page 1 of 1

php LDAP search

Posted: Mon Oct 20, 2008 11:44 am
by potato_chip
have a question about ldap_search function. Should be easy, but couldn't figure out.

here is what I want:

After first ldap_search in the company's Active Directory, I can retrieve employee's manager information, it is shown in distingushedname format, like this: CN=Kathy William,OU=MIS,OU=MIS,OU=MGM,DC=MGM,DC=com

What I really like is to display the manager's full name - displayName, so I need to run a ldap_search again baed on the manager's distinguishedname, but seems like I can not retrieve any information, the error msg always displayed:"Error: Only one entrie should be returned". Is the $filter syntax wrong???

here is my simple code:

Code: Select all

 
$base_dn = "ou=MGM,dc=MGM,dc=com";
$filter = "DN='CN=Kathy William,OU=MIS,OU=MIS,OU=MGM,DC=MGM,DC=com'";
$justthese = array("displayname");
        
$result = ldap_search($ldapconn, $base_dn, $filter, $justthese);
$entries = ldap_get_entries($ldapconn, $result);
$numEntries = $entries["count"];
if ($numEntries != 1)
    echo  "Error: Only one entrie should be returned\n<br /><br />";
else
{
    echo "Display Name: " . $entries[0]['displayname'][0] . "\n<br />";
}
 

Re: php LDAP search

Posted: Mon Oct 20, 2008 2:09 pm
by pickle
The error being generated by you - so it's not an error from PHP per se it's just that there are multiple entries returned. Try outputting $entries with print_r() (or my dump_array() function: viewtopic.php?f=29&t=42991&hilit=dump_array) and see what comes back.

I can tell you that you're probably not looking for that long DN, but rather just "Kathy Williams" - at least that's how it would work in my LDAP system.

Alternatively, you may want to use an LDAP layer: viewtopic.php?f=29&t=47448

Re: php LDAP search

Posted: Mon Oct 20, 2008 3:11 pm
by potato_chip
Thanks for the reply. My problem is "NO result at all is being returned." even after I modified the code like below. It always display "0 entries returned" while I'm sure that 1 entry should be returned! So my question really is "How to search by the distinguished name (DN) since there are so many equal signs in it "=", and I assume that is what caused the problem. (Correct me if I'm wrong.)

Code: Select all

 
$base_dn = "ou=wga,dc=wga,dc=com";
$filter = "distinguishedname='CN=Kathy Williams,OU=MIS,OU=MIS,OU=MGM,DC=MGM,DC=com'";
    
$result = ldap_search($ldapconn, $base_dn, $filter);
$entries = ldap_get_entries($ldapconn, $result);
$numEntries = $entries["count"];
echo  $numEntries . " entrie returned\n<br /><br />";
        
for($i=0; $i<$numEntries; $i++)
{
    //echo "Common Name: " . $entries[$i]['cn'][0] . "\n<br />";
    echo "Display Name: " . $entries[$i]['displayname'][0] . "\n<br />";
    echo "Distinguished Name: " . $entries[$i]['distinguishedname'][0] . "\n<br />";
    echo "Title: " . $entries[$i]['title'][0] . "\n<br />";
    echo "Description: " . $entries[$i]['description'][0] . "\n<br />";
    echo "sAMAccountName: " . $entries[$i]['samaccountname'][0] . "\n<br />";
    echo "Mail: " . $entries[$i]['mail'][0] . "\n<br />";
    echo "Department: " . $entries[$i]['department'][0] . "\n<br />";
    echo "Location: " . $entries[$i]['l'][0] . "\n<br />";
    echo "Company: " . $entries[$i]['company'][0] . "\n<br />";
    
    echo "<br /><br />";
}
 
 

Re: php LDAP search

Posted: Mon Oct 20, 2008 3:24 pm
by pickle
The problem is almost certainly with how you're asking for your information.

On my system, everyone has a username with no spaces, which makes it quite simple. I honestly don't know how to search for something that has a space - that really throws a kink in it.

How do you ask for the employee information in the first place? I imagine you'll want a similar format when asking for the manager's information.

Also, please wrap your PHP code in [syntax=php][/syntax] tags to enable syntax highlighting.

Re: php LDAP search

Posted: Mon Oct 20, 2008 3:44 pm
by potato_chip
Problem solved. I passed the wrong parameters. Feels stupid. :oops: Thanks for all the help!