I wrote a ldap query class in PHP to authenticate users. I need to query ldap server to multiple OU, this is an example:
DC=mydomain,DC=com
OU=MyBusiness
CN=MyGroup
DC=mydomain,DC=com
OU=Users
CN=Domain Users
In this domain, i have two OU, one my custom OU with a group. The other is the standard Windows OU. I would like to create a query to get both groups, the one inside OU=Users and the one inside OU=MyBusiness... But seems to be impossibile. I tried this connection:
$r=ldap_connect($ldap_host,$ldap_port);
ldap_bind($r,$user,$passw);
$domain="OU=MyBusiness,DC=mydomain,DC=com";
$sr=ldap_search($r, $domain ,"(&(objectClass=user)(sAMAccountName=".$user."))");
But gets only MyBusiness's group, I also tried these:
$domain="OU=Users,DC=mydomain,DC=com"; //gets only the other group
$domain="DC=mydomain,DC=com"; //error
$domain="OU=*,DC=mydomain,DC=com"; //error
But no one works. How can I do?
here you go - this should do it. (those ldap_options are required for top level "DC=mydomain,DC=com" searches)
$r=ldap_connect($ldap_host,$ldap_port);
ldap_set_option($r, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($r, LDAP_OPT_REFERRALS, 0);
ldap_bind($r,$user,$passw);
$search = "(&(objectClass=user)(sAMAccountName=$user))";
$attributes = array("cn","displayName","dn");
$sr=ldap_search($r,$domain,$search,$attributes);
$results = ldap_get_entries($r,$sr);
foreach ($results as $result) {
echo $result['cn'];
}