敬业的IT人 >> 网络技术 >> 网络协议 >> LDAP 目录协议函数库

LDAP 目录协议函数库

敬业的IT人 互联网 佚名 2008-3-20 10:45:13

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。

<?php
// 本例使用到 connect, bind, search, interpret search
// result, close connection 等等 LDAP 的功能。
echo "<h3>LDAP 搜寻测试</h3>" ;
echo "连接中 ..." ;
$ds = ldap_connect ( "localhost" ); // 先连上有效的 LDAP 服务器
echo "连上 " . $ds . "<p>" ;

if ( $ds ) {
echo "Binding ..." ;
$r = ldap_bind ( $ds ); // 匿名的 bind,为只读属性
echo "Bind 返回 " . $r . "<p>" ;
echo "Searching for (sn=S*) ..." ; // 找寻 S 开头的姓氏
$sr = ldap_search ( $ds , "o=My Company, c=US" , "sn=S*" );
echo "Search 返回 " . $sr . "<p>" ;
echo "S 开头的姓氏有 " . ldap_count_entries ( $ds , $sr ). " 个<p>" ;
echo "取回姓氏资料 ...<p>" ;
$info = ldap_get_entries ( $ds , $sr );
echo "资料返回 " . $info [ "count" ]. " 笔:<p>" ;
for ( $i = 0 ; $i < $info [ "count" ]; $i ++) {
echo "dn 为: " . $info [ $i ][ "dn" ] . "<br>" ;
echo "cn 为: " . $info [ $i ][ "cn" ][ 0 ] . "<br>" ;
echo "email 为: " . $info [ $i ][ "mail" ][ 0 ] . "<p>" ;
}
echo "关闭链接" ;
ldap_close ( $ds );
} else {
echo "<h4>无法连接到 LDAP 服务器</h4>" ;
}
?>

欲使用 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 名录的条目。
语法: boolean ldap_add(int handle, string dn, array entry);
返回值: 布尔值
函数种类: 网络系统
内容说明: 本函数用来加入新的条目到 LDAP 名录之中。参数 handle 为打开 LDAP 的代号。参数 dn 为要加入条目的具体 dn 字符串。参数 entry 为数组,为个体所有的条目,数组的内容是条目的相关信息。若无错误则返回 true 值。
使用范例
<?php
$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 名录的属性。
语法: boolean ldap_mod_add(int handle, string dn, array entry);
返回值: 布尔值
函数种类: 网络系统
内容说明: 本函数用来加入新的属性到 LDAP 名录之中。参数 handle 为打开 LDAP 的代号。参数 dn 为要加入条目的具体 dn 字符串。参数 entry 为数组,为个体所有的属性,数组的内容是名录属性的相关信息。若无错误则返回 true 值。
参考: ldap_modify()

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 名录取代旧属性。
语法: boolean ldap_mod_replace(int handle, string dn, array entry);
返回值: 布尔值
函数种类: 网络系统
内容说明: 本函数用来用新的指定属性取代 LDAP 名录之中的属性。参数 handle 为打开 LDAP 的代号。参数 dn 为要加入条目的具体 dn 字符串。参数 entry 为数组,为个体所有的属性,数组的内容是名录属性的相关信息。若无错误则返回 true 值。

ldap_bind
系住 LDAP 目录。
语法: boolean ldap_bind(int handle, string [bind_rdn], string [bind_password]);
返回值: 布尔值
函数种类: 网络系统
内容说明: 本函数用来系住特定的 RDN 和用户密码。参数 handle 为打开 LDAP 的代号。参数 bind_rdn 及 bind_password 可省略。当未指定用户的 RDN 及密码时,则系住匿名用
  

粤ICP备06119539号
Copyright CiscoSky.Org,Some Rights Reserved.
Email:me1228#tom.com