在PHP中可以使用LDAP(Lightweight Directory Access Protocol)來查詢用戶和組。以下是一個簡單的示例代碼:
$ldapServer = 'ldap://ldap.example.com';
$ldapPort = 389;
$ldapConn = ldap_connect($ldapServer, $ldapPort);
$ldapAdmin = 'cn=admin,dc=example,dc=com';
$ldapPassword = 'password';
ldap_bind($ldapConn, $ldapAdmin, $ldapPassword);
$ldapBaseDn = 'ou=users,dc=example,dc=com';
$ldapFilter = '(objectClass=person)';
$ldapAttributes = array('cn', 'mail');
$ldapSearch = ldap_search($ldapConn, $ldapBaseDn, $ldapFilter, $ldapAttributes);
$ldapEntries = ldap_get_entries($ldapConn, $ldapSearch);
foreach ($ldapEntries as $entry) {
echo "Username: " . $entry['cn'][0] . ", Email: " . $entry['mail'][0] . "<br>";
}
$ldapGroupBaseDn = 'ou=groups,dc=example,dc=com';
$ldapGroupFilter = '(objectClass=groupOfUniqueNames)';
$ldapGroupAttributes = array('cn', 'uniqueMember');
$ldapGroupSearch = ldap_search($ldapConn, $ldapGroupBaseDn, $ldapGroupFilter, $ldapGroupAttributes);
$ldapGroupEntries = ldap_get_entries($ldapConn, $ldapGroupSearch);
foreach ($ldapGroupEntries as $entry) {
echo "Group: " . $entry['cn'][0] . "<br>";
foreach ($entry['uniquemember'] as $member) {
echo "Member: " . $member . "<br>";
}
}
請注意,以上代碼僅供參考,具體的LDAP服務(wù)器配置和數(shù)據(jù)結(jié)構(gòu)可能會有所不同。在實際應(yīng)用中,您需要根據(jù)您的LDAP服務(wù)器的配置和數(shù)據(jù)結(jié)構(gòu)進(jìn)行相應(yīng)的調(diào)整。