正如您在下面看到的,當我對安全組執行LDAP搜索時,沒有收到任何用戶信息。我想使用$_SERVER[remote_user]
來檢查用戶是否是該組的成員。我也想檢索這個用戶的信息並用它更新sql數據庫。這可能嗎?如何使用LDAP和PHP檢索Active Directory安全組中的用戶信息
$dn = "CN=Intra,OU=Common Security Groups,DC=mydomain,DC=local";
$filter = "(member=*)";
$ad = ldap_connect("IP") or die("Couldn't connect to AD!");
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
$bd = ldap_bind($ad, "[email protected]", "password") or die("Can't bind to server.");
$sr = ldap_search($ad,$dn,$filter);
$entries = ldap_get_entries($ad, $sr);
print_r($entries);
返回此:當我用正常的DN
Array
(
[count] => 1
[0] => Array
(
[objectclass] => Array
(
[count] => 2
[0] => top
[1] => group
)
[0] => objectclass
[cn] => Array
(
[count] => 1
[0] => Intra
)
[1] => cn
[description] => Array
(
[count] => 1
[0] => Group for (LDAP) INTRANET server access
)
[2] => description
[member] => Array
(
[count] => 4
[0] => CN=Fname1 Lname1,OU=Mail enabled users,OU=Aberdeen,DC=mydomain,DC=local
[1] => CN=Fname2 Lname2,OU=Mail enabled users,OU=Forres,DC=mydomain,DC=local
[2] => CN=Fname3 Lname3,OU=Houston,DC=mydomain,DC=local
[3] => CN=Fname4 Lname4,OU=Mail enabled users,OU=Bergen,DC=mydomain,DC=local
)
[3] => member
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Intra,OU=Common Security Groups,DC=mydomain,DC=local
)
[4] => distinguishedname
[instancetype] => Array
(
[count] => 1
[0] => 4
)
[5] => instancetype
[whencreated] => Array
(
[count] => 1
[0] => 20100711172407.0Z
)
[6] => whencreated
[whenchanged] => Array
(
[count] => 1
[0] => 20100712063949.0Z
)
[7] => whenchanged
[usncreated] => Array
(
[count] => 1
[0] => 17491499
)
[8] => usncreated
[usnchanged] => Array
(
[count] => 1
[0] => 17498823
)
[9] => usnchanged
[name] => Array
(
[count] => 1
[0] => Intra
)
[10] => name
[objectguid] => Array
(
[count] => 1
[0] =>
)
[11] => objectguid
[objectsid] => Array
(
[count] => 1
[0] =>
)
[12] => objectsid
[samaccountname] => Array
(
[count] => 1
[0] => Intra
)
[13] => samaccountname
[samaccounttype] => Array
(
[count] => 1
[0] => 268435456
)
[14] => samaccounttype
[grouptype] => Array
(
[count] => 1
[0] => -2147483646
)
[15] => grouptype
[objectcategory] => Array
(
[count] => 1
[0] => CN=Group,CN=Schema,CN=Configuration,DC=mydomain,DC=local
)
[16] => objectcategory
[count] => 17
[dn] => CN=Intra,OU=Common Security Groups,DC=mydomain,DC=local
)
)
一切正常:
$dn = "OU=Mail enabled users,OU=Bergen,DC=mydomain,DC=local";
但AD專家告訴我,這是一個很大的NO -NO,我應該使用安全組代替:\
感謝您的快速響應!但是當我嘗試使用你的過濾器時,我得到這個錯誤:PHP警告:ldap_search()[function.ldap-search]:搜索:操作錯誤在C:\ inetpub \ wwwroot \ test \ test.php 33行PHP警告: ldap_get_entries():提供的參數不是在第35行的C:\ inetpub \ wwwroot \ test \ test.php中的有效ldap結果資源。 – horgen 2010-07-13 10:17:25
由於我看到您在Windows上,因此提示。下載Softerra LDAP Browser 2.6(它是免費的),將它連接到您的AD並使用該工具測試您的搜索。 - 理論上,''DC = mydomain,DC = local「的基本DN應該可以工作。 – Tomalak 2010-07-13 12:47:20
謝謝。現在我不知道到底發生了什麼。我搜索了DN:「DC = mydomain,DC = local」FILTER:「(&(objectCategory = user)(memberOf = CN = Intra,OU = Common Security Groups,DC = mydomain,DC = local))」in完全沒有問題的LDAP瀏覽器。但是當我嘗試在PHP中執行相同的操作時,出現此錯誤:「PHP警告:ldap_search()[function.ldap-search]:搜索:C:\ inetpub \ wwwroot \ test \ test.php中的操作錯誤17「:(我沒有得到任何錯誤,如果我使用」OU =卑爾根,DC = mydomain,DC =本地「,但是我必須爲每個OU創建一個for循環 – horgen 2010-07-14 07:52:28