函式名:ldap_read()
適用版本:PHP 4, PHP 5, PHP 7
用法:ldap_read() 函式用於在LDAP目錄中搜尋條目。它執行指定的搜尋,並返回所有匹配條目的結果識別符號。
語法:ldap_read ( resource $ldap_conn , string $base_dn , string $filter [, array $attributes [, int $attrsonly [, int $sizelimit [, int $timelimit [, int $deref ]]]]] ) : resource|false
引數:
- $ldap_conn:一個連線識別符號,由 ldap_connect() 函式返回。
- $base_dn:搜尋的基準DN(Distinguished Name)。搜尋將從此DN開始。
- $filter:LDAP搜尋過濾器。使用此過濾器來匹配要搜尋的條目。
- $attributes(可選):要返回的屬性陣列。如果未指定,則返回所有屬性。
- $attrsonly(可選):如果設定為 1,則只返回屬性名稱,而不返回屬性值。
- $sizelimit(可選):限制結果集的條目數。
- $timelimit(可選):限制搜尋的時間(秒)。
- $deref(可選):指定解引用別名的方式。
返回值:成功則返回一個結果識別符號,失敗則返回 false。
示例:
// 建立與LDAP伺服器的連線
$ldap_conn = ldap_connect("ldap.example.com");
// 繫結到LDAP伺服器(可選)
ldap_bind($ldap_conn, "cn=admin,dc=example,dc=com", "password");
// 執行搜尋
$result = ldap_read($ldap_conn, "ou=Users,dc=example,dc=com", "(objectClass=person)");
// 檢查搜尋結果
if ($result) {
// 獲取搜尋結果的第一個條目
$entry = ldap_first_entry($ldap_conn, $result);
// 獲取條目的 DN
$dn = ldap_get_dn($ldap_conn, $entry);
// 獲取條目的屬性值
$attributes = ldap_get_attributes($ldap_conn, $entry);
// 列印結果
echo "DN: " . $dn . "\n";
echo "Attributes: \n";
print_r($attributes);
} else {
echo "搜尋失敗!";
}
// 關閉 LDAP 連線
ldap_close($ldap_conn);
以上示例演示瞭如何使用 ldap_read() 函式在 LDAP 目錄中搜尋條目。首先建立與 LDAP 伺服器的連線,然後執行搜尋,並獲取搜尋結果的第一個條目。最後,獲取條目的 DN 和屬性值,並列印出來。請注意,示例中的引數值僅供參考,實際使用時需要根據實際情況進行調整。