我可以使用php成功连接并绑定到windows AD中的ldap。但是,我没有从 ldap_search 获得预期的结果。我知道我的测试用户在 Intranet_Group123 中,但是 ldap_search 没有找到该组,或者我不知道如何正确解析结果数组。我知道组的可分辨名称:CN=Intranet_Group123,OU=PD,OU=Intranet,OU=Apps,OU=Groups,DC=mydomain,DC=com
这是我正在使用的代码(基于此处的博客:此处的博客)。
$user = 'testuser';
$password = 'test';
$ldap_host = "myServer";
// Active Directory DN (I have also tried having CN=Intranet_Group123 at the beginning of the string)
$ldap_dn = "OU=PD,OU=Intranet,OU=Apps,OU=Groups,DC=mydomain,DC=com";
// Domain, for purposes of constructing $user
$ldap_usr_dom = "@mydomain.com";
// connect to active directory
$ldap = ldap_connect($ldap_host);
// verify user and password
if($bind = @ldap_bind($ldap, $user . $ldap_usr_dom, $password)) {
var_dump($bind); // bool•true
// valid
// check presence in groups
$filter = "(sAMAccountName=" . $user . ")";
$attr = array("memberof");
$result = ldap_search($ldap, $ldap_dn, $filter, $attr) or exit("Unable to search LDAP server");
var_dump($result); //resource(8) of type (ldap result)
$entries = ldap_get_entries($ldap, $result);
var_dump($entries); // array(1) {'count' → int 0}