查詢

ldap_sasl_bind()函式—用法及示例

「 繫結到LDAP伺服器並使用SASL進行身份驗證 」


函式名:ldap_sasl_bind()

適用版本:PHP 5 >= 5.1.0, PHP 7, PHP 8

用法: ldap_sasl_bind(resource $ldap, ?string $binddn = null, ?string $password = null, ?string $sasl_mech = null, ?string $sasl_realm = null, ?string $sasl_authc_id = null, ?string $sasl_authz_id = null, ?string $props = null): bool

該函式用於繫結到LDAP伺服器並使用SASL進行身份驗證。SASL(Simple Authentication and Security Layer)是一種用於身份驗證和安全性的標準框架。

引數:

  • $ldap:LDAP連線識別符號,透過ldap_connect()函式獲取。
  • $binddn(可選):繫結的DN(Distinguished Name),即使用者名稱。
  • $password(可選):繫結的密碼。
  • $sasl_mech(可選):SASL機制名稱,例如"PLAIN"、"GSSAPI"等。如果未指定,則使用伺服器的首選機制。
  • $sasl_realm(可選):SASL領域,用於指定要使用的領域。
  • $sasl_authc_id(可選):SASL認證ID,用於指定要用於認證的ID。
  • $sasl_authz_id(可選):SASL授權ID,用於指定要用於授權的ID。
  • $props(可選):其他SASL屬性,以逗號分隔的鍵值對形式傳遞。

返回值:

  • 成功時返回true,失敗時返回false。

示例:

$ldap = ldap_connect('ldap.example.com');
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);

if (ldap_sasl_bind($ldap, 'cn=admin,dc=example,dc=com', 'password', 'PLAIN')) {
    echo "LDAP bind successful";
} else {
    echo "LDAP bind failed";
}

以上示例中,我們首先使用ldap_connect()函式建立與LDAP伺服器的連線。然後,使用ldap_set_option()函式設定LDAP連線的協議版本為3。接下來,我們使用ldap_sasl_bind()函式進行繫結操作。指定了繫結的DN('cn=admin,dc=example,dc=com')和密碼('password'),並指定了SASL機制為'PLAIN'。最後根據繫結操作的結果輸出相應的訊息。

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