GB | BIG5
|
| 首頁 > 編程技術 > Php > 正文 |
 |
| PHP4手冊:函數庫及函數(二十四) LDAP 目錄協定函式庫 |
| http://netleader.126.com 星空浪子 (2001-04-18 14:41:15) |
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
--------------------------------------------------------------------------------
本函式庫共有 25 個函式
LDAP (Lightweight Directory Access Protocol) 是一種輕量的目錄存取協定,提供客戶從各個角落連接到目錄伺服器中。在 RFC 1777 及 RFC 1778 中對 LDAP 有較深入的描述,亦可參考 Netscape 站上有關 LDAP 方面的資料。
在 LDAP 的協定之中,很像硬碟目錄結構或倒過來的樹狀結構。LDAP 的根就是全世界,第一級是屬國別 (countries) 性質的層級,之可能會有公司 (organization) 的層級,接著是部門 (organizationalUnit),再來為個人。而就像檔案,每個人都會有所謂的顯名 (distinguished name, 簡稱 dn),dn 可能像醬子 cn=John Smith,ou=Accounts,o=My Company,c=US。
// 本例使用到 connect, bind, search, interpret search
// result, close connection 等等 LDAP 的功能。
echo "LDAP 搜尋測試";
echo "連線中 ...";
$ds=ldap_connect("localhost"); // 先連上有效的 LDAP 伺服器
echo "連上 ".$ds."";
if ($ds) {
echo "Binding ...";
$r=ldap_bind($ds); // 匿名的 bind,為唯讀屬性
echo "Bind 傳回 ".$r." ";
echo "Searching for (sn=S*) ..."; // 找尋 S 開頭的姓氏
$sr=ldap_search($ds,"o=My Company, c=US", "sn=S*");
echo "Search 傳回 ".$sr." ";
echo "S 開頭的姓氏有 ".ldap_count_entries($ds,$sr)." 個 ";
echo "取回姓氏資料 ... ";
$info = ldap_get_entries($ds, $sr);
echo "資料傳回 ".$info["count"]." 筆: ";
for ($i=0; $i<$info["count"]; $i++) {
echo "dn 為: ". $info[$i]["dn"] ." ";
echo "cn 為: ". $info[$i]["cn"][0] ." ";
echo "email 為: ". $info[$i]["mail"][0] ." ";
}
echo "關閉連結";
ldap_close($ds);
} else {
echo " 無法連接到 LDAP 伺服器";
}
?>
欲使用 LDAP 伺服器功能要先在 Web 伺服器安裝 LDAP 客戶端程式,較著名的有美國密西根大學的 ldap-3.3 套件或者是 Netscape 的 Directory SDK。可到下列網址找回來安裝
Netscape http://developer.netscape.com/tech/directory/
密西根大學 http://www.umich.edu/~dirsvcs/ldap/index.html
OpenLDAP 計劃 http://www.openldap.com
LDAP World http://elvira.innosoft.com/ldapworld
ldap_add: 增加 LDAP 名錄的條目。
ldap_mod_add: 增加 LDAP 名錄的屬性。
ldap_mod_del: 刪除 LDAP 名錄的屬性。
ldap_mod_replace: 新的 LDAP 名錄取代舊屬性。
ldap_bind: 系住 LDAP 目錄。
ldap_close: 結束 LDAP 連結。
ldap_connect: 連上 LDAP 伺服器。
ldap_count_entries: 搜尋結果的數目。
ldap_delete: 刪除指定資源。
ldap_dn2ufn: 將 dn 轉成易讀的名字。
ldap_explode_dn: 切開 dn 的欄位。
ldap_first_attribute: 取得第一筆資源的屬性。
ldap_first_entry: 取得第一筆結果代號。
ldap_free_result: 釋放傳回資料記憶體。
ldap_get_attributes: 取得傳回資料的屬性。
ldap_get_dn: 取得 DN 值。
ldap_get_entries: 取得全部傳回資料。
ldap_get_values: 取得全部傳回值。
ldap_list: 列出簡表。
ldap_modify: 改變 LDAP 名錄的屬性。
ldap_next_attribute: 取得傳回資料的下筆屬性。
ldap_next_entry: 取得下一筆結果代號。
ldap_read: 取得目前的資料屬性。
ldap_search: 列出樹狀簡表。
ldap_unbind: 結束 LDAP 連結。
--------------------------------------------------------------------------------
函式:ldap_add()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_add
增加 LDAP 名錄的條目。
語法: boolean ldap_add(int handle, string dn, array entry);
傳回值: 布林值
函式種類: 網路系統
內容說明
本函式用來加入新的條目到 LDAP 名錄之中。參數 handle 為開啟 LDAP 的代號。參數 dn 為要加入條目的具體 dn 字串。參數 entry 為陣列,為個體所有的條目,陣列的內容是條目的相關資訊。若無錯誤則傳回 true 值。
使用范例
$ds=ldap_connect("localhost"); // 連上 LDAP 伺服器
if ($ds) {
// 系住恰當的 dn
$r=ldap_bind($ds,"cn=root, o=A2Z Company, c=TW", "secret");
// 預先準備好新條目的資料
$info["cn"]="Wilson Peng";
$info["sn"]="Peng";
$info["mail"]="wilson@wilson.gs";
$info["objectclass"]="person";
// 加入新條目
$r=ldap_add($ds, "cn=Wilson Peng, o=A2Z Company, c=TW", $info);
ldap_close($ds);
} else {
echo "抱歉,無法連上 LDAP 伺服器。";
}
?>
--------------------------------------------------------------------------------
函式:ldap_mod_add()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_mod_add
增加 LDAP 名錄的屬性。
語法: boolean ldap_mod_add(int handle, string dn, array entry);
傳回值: 布林值
函式種類: 網路系統
內容說明
本函式用來加入新的屬性到 LDAP 名錄之中。參數 handle 為開啟 LDAP 的代號。參數 dn 為要加入條目的具體 dn 字串。參數 entry 為陣列,為個體所有的屬性,陣列的內容是名錄屬性的相關資訊。若無錯誤則傳回 true 值。
參考
ldap_modify()
--------------------------------------------------------------------------------
函式:ldap_mod_del()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_mod_del
刪除 LDAP 名錄的屬性。
語法: boolean ldap_mod_del(int handle, string dn, array entry);
傳回值: 布林值
函式種類: 網路系統
內容說明
本函式用來刪除到 LDAP 名錄之中的指定屬性。參數 handle 為開啟 LDAP 的代號。參數 dn 為要加入條目的具體 dn 字串。參數 entry 為陣列,為個體所有的屬性,陣列的內容是名錄屬性的相關資訊。若無錯誤則傳回 true 值。
--------------------------------------------------------------------------------
函式:ldap_mod_replace()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_mod_replace
新的 LDAP 名錄取代舊屬性。
語法: boolean ldap_mod_replace(int handle, string dn, array entry);
傳回值: 布林值
函式種類: 網路系統
內容說明
本函式用來用新的指定屬性取代 LDAP 名錄之中的屬性。參數 handle 為開啟 LDAP 的代號。參數 dn 為要加入條目的具體 dn 字串。參數 entry 為陣列,為個體所有的屬性,陣列的內容是名錄屬性的相關資訊。若無錯誤則傳回 true 值
--------------------------------------------------------------------------------
函式:ldap_bind()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_bind
系住 LDAP 目錄。
語法: boolean ldap_bind(int handle, string [bind_rdn], string [bind_password]);
傳回值: 布林值
函式種類: 網路系統
內容說明
本函式用來系住特定的 RDN 和使用者密碼。參數 handle 為開啟 LDAP 的代號。參數 bind_rdn 及 bind_password 可省略。當未指定使用者的 RDN 及密碼時,則系住匿名使用者。若無錯誤則傳回 true 值。
--------------------------------------------------------------------------------
函式:ldap_close()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_close
結束 LDAP 連結。
語法: boolean ldap_close(int handle);
傳回值: 布林值
函式種類: 網路系統
內容說明
本函式將關閉與 LDAP 伺服器的連結。參數 handle 為開啟 LDAP 的代號。實際上這個函式就是 ldap_unbind() 的別名而已。若無錯誤則傳回 true 值。
參考
ldap_unbind()
--------------------------------------------------------------------------------
函式:ldap_connect()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_connect
連上 LDAP 伺服器。
語法: int ldap_connect(string [hostname], int [port]);
傳回值: 整數
函式種類: 網路系統
內容說明
本函式用來連結 LDAP 的伺服器。參數 hostname 若省略,則傳回現在的連結代號,也就是 handle。參數 port 亦可省略,內定值為 389。執行若無錯誤則傳回連結代號,發生錯誤則傳回 false。
--------------------------------------------------------------------------------
函式:ldap_count_entries()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_count_entries
搜尋結果的數目。
語法: int ldap_count_entries(int handle, int result_identifier);
傳回值: 整數
函式種類: 網路系統
內容說明
本函式用來取得搜尋的結果數目。參數 handle 為開啟 LDAD 伺服器的代號。參數 result_identifier 為 ldap_search() 所傳回的搜尋代號。執行若無錯誤則傳回尋找到的數目,發生錯誤則傳回 false。
--------------------------------------------------------------------------------
函式:ldap_delete()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_delete
刪除指定資源。
語法: boolean ldap_delete(int handle, string dn);
傳回值: 布林值
函式種類: 網路系統
內容說明
本函式用來刪除指定 dn 的資料。參數 handle 為開啟 LDAD 伺服器的代號。參數 dn 為指定的顯名 (distinguished name)。發生錯誤則傳回 false,若無錯誤則傳回 true。
--------------------------------------------------------------------------------
函式:ldap_dn2ufn()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_dn2ufn
將 dn 轉成易讀的名字。
語法: string ldap_dn2ufn(string dn);
傳回值: 字串
函式種類: 網路系統
內容說明
本函式用來轉換 dn 的名字成為較易讀取的名字 ufn (User Friendly Name)。參數 dn 為欲轉換的字串。傳回值即為 ufn 字串
--------------------------------------------------------------------------------
函式:ldap_explode_dn()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_explode_dn
切開 dn 的欄位。
語法: array ldap_explode_dn(string dn, int attrib);
傳回值: 陣列
函式種類: 網路系統
內容說明
本函式將 dn 中的各欄位切開,切開的每個欄位即為 RDN (Relative Distinguished Name)。參數 dn 為欲切開的字串。參數 attrib 若為 0 表示傳回該欄的屬性,若為 1 則表示傳回該欄的值。傳回值置陣列之中。
--------------------------------------------------------------------------------
函式:ldap_first_attribute()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_first_attribute
取得第一筆資源的屬性。
語法: string ldap_first_attribute(int handle, int result_entry_identifier, int ber_identifier);
傳回值: 字串
函式種類: 網路系統
內容說明
本函式用來取得 LDAP 中第一筆資源的屬性。參數 handle 為開啟 LDAP 的代號。參數 result_entry_identifier 將由 ldap_next_attribute() 沿用。參數 ber_identifier 為指標,意即需在前面加上 & 符號。
參考
ldap_get_attributes() ldap_next_attribute()
--------------------------------------------------------------------------------
函式:ldap_first_entry()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_first_entry
取得第一筆結果代號。
語法: int ldap_first_entry(int handle, int result_identifier);
傳回值: 整數
函式種類: 網路系統
內容說明
本函式用來取得 LDAP 中第一筆結果的代號 ID。參數 handle 為開啟 LDAP 的代號。參數 result_identifier 為 ldap_search() 所傳回的搜尋代號。若有錯誤則傳回 false。
參考
ldap_get_entries()
--------------------------------------------------------------------------------
函式:ldap_free_result()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_free_result
釋放傳回資料記憶體。
語法: int ldap_free_result(int result_identifier);
傳回值: 整數
函式種類: 網路系統
內容說明
本函式用來釋放傳回資料所使用的記憶體。若沒有使用本函式釋放記憶體,程式結束時也會自動釋放。
--------------------------------------------------------------------------------
函式:ldap_get_attributes()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_get_attributes
取得傳回資料的屬性。
語法: array ldap_get_attributes(int handle, int result_entry_identifier);
傳回值: 陣列
函式種類: 網路系統
內容說明
本函式用來取得 LDAP 中所有資源的屬性。參數 handle 為開啟 LDAP 的代號。參數 result_entry_identifier 將由 ldap_next_attribute() 沿用。傳回值為陣列資料,陣列元素從零開始依次為屬性的值。例如
return_value["count"] : 屬性個數
return_value[0] : 第一個屬性
return_value[n] : 第n+1個屬性
return_value["attribute"]["count"] : 屬性值個數
return_value["attribute"][0] : 第一個屬性值
return_value["attribute"][i] : 第i+1個屬性值
使用范例
下面范例為片段的程式
$entry = ldap_first_entry($ds, $sr);
$attrs = ldap_get_attributes($ds, $entry);
echo $attrs["count"]." 筆屬性資料如下:\n";
for ($i=0; $i<$attrs["count"]; $i++) {
echo $attrs[$i]." ";
}
?>
參考
ldap_first_attribute() ldap_next_attribute()
--------------------------------------------------------------------------------
函式:ldap_get_dn()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_get_dn
取得 DN 值。
語法: string ldap_get_dn(int handle, int result_entry_identifier);
傳回值: 字串
函式種類: 網路系統
內容說明
本函式用來取得 LDAP 的顯名 (DN, distinguished name)字串值。參數 handle 為開啟 LDAP 的代號。參數 result_identifier 為 ldap_search() 所傳回的搜尋代號。若有錯誤則傳回 false。
--------------------------------------------------------------------------------
函式:ldap_get_entries()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_get_entries
取得全部傳回資料。
語法: array ldap_get_entries(int handle, int result_identifier);
傳回值: 陣列
函式種類: 網路系統
內容說明
本函式用來取得 LDAP 的全部傳回資料。參數 handle 為開啟 LDAP 的代號。傳回值為陣列資料,例如
return_value["count"] : 傳回資料筆數
return_value[0] : 第一筆傳回資料
return_value[i]["dn"] : 第 i+1 筆資料的 DN
return_value[i]["count"] : 第 i+1 筆資料的數目
return_value[i][j] : 第 i+1 筆資料的第 j 值
return_value[i]["attribute"]["count"] : 第 i+1 筆資料的屬性數
return_value[i]["attribute"][j] : 第 i+1 筆資料的第 j 個屬性
參考
ldap_first_entry() ldap_next_entry()
--------------------------------------------------------------------------------
函式:ldap_get_values()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_get_values
取得全部傳回值。
語法: array ldap_get_values(int handle, int result_entry_identifier, string attribute);
傳回值: 陣列
函式種類: 網路系統
內容說明
本函式用來取得 LDAP 的全部傳回資料值。參數 handle 為開啟 LDAP 的代號。參數 attribute 有 surname 及 mail 二種。傳回值為陣列資料值,例如
return_value["count"] : 屬性值總數
return_value[0] : 第一個屬性值
return_value[i] : 第 i+1 個屬性值
使用范例
下例為片段程式
$values = ldap_get_values($ds, $entry, "mail");
echo $values["count"]." 筆電子郵件地址: \n";
for ($i=0; $i < $values["count"]; $i++) {
echo $values[$i]." ";
}
?>
--------------------------------------------------------------------------------
函式:ldap_list()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_list
列出簡表。
語法: int ldap_list(int handle, string base_dn, string filter, array [attributes]);
傳回值: 整數
函式種類: 網路系統
內容說明
本函式用來列出單層 (One Level) 的資料,實際上它等ldap_searchldap_search(),只是將值域改成 LDAP_SCOPE_ONELEVEL,本函式作用有點像 DOS 的 dir 或是 UNIX 的 ls。參數 handle 為開啟 LDAP 的代號。參數 base_dn 為最基本的 dn 條件值,例如包括 o 和 c 二欄位。參數 filter 為布林條件,它的語法可以在 Netscape 站上找一份 dirsdkpg.pdf 檔案,其中的 Search Syntax 一部份有詳細的說明。參數 attributes 可省略,用來設定更細的列出屬性。
使用范例
這個范例為片段程式
$basedn = "o=SuperPHP Company, c=TW";
$justthese = array("ou");
$sr=ldap_list($ds, $basedn, "ou=*", $justthese);
$info = ldap_get_entries($ds, $sr);
for ($i=0; $i<$info["count"]; $i++) {
echo $info[$i]["ou"][0] ;
}
?>
參考
ldap_read() ldap_search()
--------------------------------------------------------------------------------
函式:ldap_modify()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_modify
改變 LDAP 名錄的屬性。
語法: boolean ldap_modify(int handle, string dn, array entry);
傳回值: 布林值
函式種類: 網路系統
內容說明
本函式用來改變 LDAP 伺服器上目前所在名錄上的屬性。參數 handle 為開啟 LDAP 的代號。參數 dn 為要加入條目的具體 dn 字串。參數 entry 為陣列,為個體所有的屬性,陣列的內容是名錄屬性的相關資訊。若無錯誤則傳回 true 值。
使用范例
本例為 nickt@powys.gov.uk 提出的片段 (23-Apr-1999)
$newinfo[mail]="nick@county.gov.uk";
ldap_modify($valid_ldaplink, $valid_dn, $newinfo);
?>
參考
ldap_mod_add()
--------------------------------------------------------------------------------
函式:ldap_next_attribute()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_next_attribute
取得傳回資料的下筆屬性。
語法: string ldap_next_attribute(int handle, int result_entry_identifier, int ber_identifier);
傳回值: 字串
函式種類: 網路系統
內容說明
本函式用來取得 LDAP 中所在傳回的下筆屬性。參數 handle 為開啟 LDAP 的代號。參數 result_entry_identifier 將由 ldap_next_attribute() 沿用。參數 ber_identifier 為指標,意即需在前面加上 & 符號。
參考
ldap_get_attributes() ldap_first_attribute()
--------------------------------------------------------------------------------
函式:ldap_next_entry()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_next_entry
取得下一筆結果代號。
語法: int ldap_next_entry(int handle, int result_entry_identifier);
傳回值: 整數
函式種類: 網路系統
內容說明
本函式用來取得 LDAP 中目前指標的下筆資料代號。參數 handle 為開啟 LDAP 的代號。參數 result_identifier 為 ldap_search() 所傳回的搜尋代號。若有錯誤則傳回 false。
參考
ldap_first_entry() ldap_get_entries()
--------------------------------------------------------------------------------
函式:ldap_read()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_read
取得目前的資料屬性。
語法: int ldap_read(int handle, string base_dn, string filter, array [attributes]);
傳回值: 整數
函式種類: 網路系統
內容說明
本函式用來列出樹狀資料,它的值域條件是 LDAP_SCOPE_BASE。參數 handle 為開啟 LDAP 的代號。參數 base_dn 為最基本的 dn 條件值,例如包括 o 和 c 二欄位。參數 filter 為布林條件,它的語法可以在 Netscape 站上找一份 dirsdkpg.pdf 檔案,其中的 Search Syntax 一部份有詳細的說明。參數 attributes 可省略,用來設定更細的列出屬性。
參考
ldap_list() ldap_search()
--------------------------------------------------------------------------------
函式:ldap_search()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_search
列出樹狀簡表。
語法: int ldap_search(int handle, string base_dn, string filter, array [attributes]);
傳回值: 整數
函式種類: 網路系統
內容說明
本函式用來列出樹狀資料,它的值域條件是 LDAP_SCOPE_SUBTREE。參數 handle 為開啟 LDAP 的代號。參數 base_dn 為最基本的 dn 條件值,例如包括 o 和 c 二欄位。參數 filter 為布林條件,它的語法可以在 Netscape 站上找一份 dirsdkpg.pdf 檔案,其中的 Search Syntax 一部份有詳細的說明。參數 attributes 可省略,用來設定更細的列出屬性。
使用范例
下面范例為片段程式
$dn = "o=SuperLDAP Company, c=TW";
$filter="(|(sn=$person*)(givenname=$person*))";
$justthese = array( "ou", "sn", "givenname", "mail");
$sr=ldap_search($ds, $dn, $filter, $justthese);
$info = ldap_get_entries($ds, $sr);
print $info["count"]." entries returned ";
?>
參考
ldap_list() ldap_read()
--------------------------------------------------------------------------------
函式:ldap_unbind()
--------------------------------------------------------------------------------
LDAP 目錄協定函式庫
ldap_unbind
結束 LDAP 連結。
語法: boolean ldap_unbind(int handle);
傳回值: 布林值
函式種類: 網路系統
內容說明
本函式將關閉與 LDAP 伺服器的連結。參數 handle 為開啟 LDAP 的代號。若無錯誤則傳回 true 值。
參考
ldap_close()
(http://www.fanqiang.com)
進入【UNIX論壇】
|
|
| 相關文章 |
PHP4手冊:函數庫及函數(四十六) SNMP 網管函式庫 (2001-04-18 16:56:55) PHP4手冊:函數庫及函數(四十五) Solid 資料庫連結函式庫 (2001-04-18 16:54:16) PHP4手冊:函數庫及函數(四十四) 信號與共享記憶體函式庫 (2001-04-18 16:49:09) PHP4手冊:函數庫及函數(四十三) 常規表示法函式庫 (2001-04-18 16:40:33) PHP4手冊:函數庫及函數(四十二) URL 處理函式庫 (2001-04-18 16:37:13) PHP4手冊:函數庫及函數(四十一) PostgreSQL 資料庫函式庫 (2001-04-18 15:58:12) PHP4手冊:函數庫及函數(四十) PDF 格式檔案函式庫 - 2 (2001-04-18 15:53:53) PHP4手冊:函數庫及函數(四十) PDF 格式檔案函式庫 - 1 (2001-04-18 15:53:35) PHP4手冊:函數庫及函數(三十九) Perl 相容語法函式庫 (2001-04-18 15:45:08) PHP4手冊:函數庫及函數(三十八) Oracle 資料庫函式庫 (2001-04-18 15:43:09)
|
===更多相關=== |
|
|
 |
★ 樊強制作 歡迎分享 ★ |