查詢

ldap_get_attributes()函式—用法及示例

「 從一個條目的搜尋結果中獲取屬性資訊 」


函式名稱:ldap_get_attributes()

適用版本:PHP 4, PHP 5, PHP 7

函式描述:ldap_get_attributes() 函式從一個條目的搜尋結果中獲取屬性資訊。

語法:array ldap_get_attributes ( resource $link_identifier , resource $result_entry_identifier )

引數:

  • $link_identifier:LDAP 連線識別符號,透過 ldap_connect() 函式獲取。
  • $result_entry_identifier:LDAP 搜尋結果中的一個條目識別符號,透過 ldap_search() 函式獲取。

返回值:成功時返回一個關聯陣列,包含了條目的屬性資訊;失敗時返回 FALSE。

示例:

// 連線到 LDAP 伺服器
$ldapconn = ldap_connect("ldap.example.com");

if ($ldapconn) {
    // 繫結到 LDAP 伺服器
    $ldapbind = ldap_bind($ldapconn, "cn=admin,dc=example,dc=com", "password");
    
    if ($ldapbind) {
        // 執行搜尋操作
        $result = ldap_search($ldapconn, "dc=example,dc=com", "cn=username");
        
        // 獲取第一個搜尋結果條目的屬性資訊
        $entry = ldap_first_entry($ldapconn, $result);
        $attributes = ldap_get_attributes($ldapconn, $entry);
        
        // 輸出屬性資訊
        echo "DN: " . ldap_get_dn($ldapconn, $entry) . "<br>";
        echo "cn: " . $attributes['cn'][0] . "<br>";
        echo "email: " . $attributes['mail'][0] . "<br>";
        
        // 關閉 LDAP 連線
        ldap_close($ldapconn);
    }
}

上述示例中,我們首先連線到 LDAP 伺服器,然後繫結到伺服器以進行操作。接著執行一個搜尋操作,並獲取第一個搜尋結果條目的屬性資訊。最後,我們輸出了該條目的 DN、cn 和 email 屬性的值。請注意,這裡的示例僅用於演示目的,實際情況中需要根據自己的 LDAP 伺服器和資料結構進行相應調整。

補充糾錯
上一個函式: ldap_get_dn()函式
下一個函式: ldap_free_result()函式
熱門PHP函式
分享連結