溫馨提示×

php中l(wèi)dap怎么查詢用戶和組

PHP
小億
192
2024-05-21 17:37:28
欄目: 編程語言

在PHP中可以使用LDAP(Lightweight Directory Access Protocol)來查詢用戶和組。以下是一個簡單的示例代碼:

  1. 連接LDAP服務(wù)器:
$ldapServer = 'ldap://ldap.example.com';
$ldapPort = 389;
$ldapConn = ldap_connect($ldapServer, $ldapPort);
  1. 綁定LDAP管理員賬號:
$ldapAdmin = 'cn=admin,dc=example,dc=com';
$ldapPassword = 'password';
ldap_bind($ldapConn, $ldapAdmin, $ldapPassword);
  1. 查詢用戶:
$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>";
}
  1. 查詢組:
$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)整。

0