帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > PHP编程
LDAP 目录协议函数库
作者:佚名 发布时间:2005-04-02 来源:不详
 

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 及密码时,则系住匿名用户。若无错误则返回 true 值。

ldap_close
结束 LDAP 链接。
语法: boolean ldap_close(int handle);
返回值: 布尔值
函数种类: 网络系统
内容说明: 本函数将关闭与 LDAP 服务器的链接。参数 handle 为打开 LDAP 的代号。实际上这个函数就是 ldap_unbind() 的别名而已。若无错误则返回 true 值。
参考: ldap_unbind()

ldap_connect
连上 LDAP 服务器。
语法: int ldap_connect(string [hostname], int [port]);
返回值: 整数
函数种类: 网络系统
内容说明: 本函数用来链接 LDAP 的服务器。参数 hostname 若省略,则返回现在的链接代号,也就是 handle。参数 port 亦可省略,默认值为 389。执行若无错误则返回链接代号,发生错误则返回 false。

ldap_count_entries
搜寻结果的数目。
语法: int ldap_count_entries(int handle, int result_identifier);
返回值: 整数
函数种类: 网络系统
内容说明: 本函数用来取得搜寻的结果数目。参数 handle 为打开 LDAD 服务器的代号。参数 result_identifier 为 ldap_search() 所返回的搜寻代号。执行若无错误则返回寻找到的数目,发生错误则返回 false。

ldap_delete
删除指定资源。
语法: boolean ldap_delete(int handle, string dn);
返回值: 布尔值
函数种类: 网络系统
内容说明: 本函数用来删除指定 dn 的资料。参数 handle 为打开 LDAD 服务器的代号。参数 dn 为指定的显名 (distinguished name)。发生错误则返回 false,若无错误则返回 true。

ldap_dn2ufn
将 dn 转成易读的名字。
语法: string ldap_dn2ufn(string dn);
返回值: 字符串
函数种类: 网络系统
内容说明: 本函数用来转换 dn 的名字成为较易读取的名字 ufn (User Friendly Name)。参数 dn 为欲转换的字符串。返回值即为 ufn 字符串。

ldap_explode_dn
切开 dn 的字段。
语法: array ldap_explode_dn(string dn, int attrib);
返回值: 数组
函数种类: 网络系统
内容说明: 本函数将 dn 中的各字段切开,切开后的每个字段即为 RDN (Relative Distinguished Name)。参数 dn 为欲切开的字符串。参数 attrib 若为 0 表示返回该栏的属性,若为 1 则表示返回该栏的值。返回值置于数组之中。

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
取得第一笔结果代号。
语法: int ldap_first_entry(int handle, int result_identifier);
返回值: 整数
函数种类: 网络系统
内容说明: 本函数用来取得 LDAP 中第一笔结果的代号 ID。参数 handle 为打开 LDAP 的代号。参数 result_identifier 为 ldap_search() 所返回的搜寻代号。若有错误则返回 false。
参考: ldap_get_entries()

ldap_free_result
释放返回资料内存。
语法: int ldap_free_result(int result_identifier);
返回值: 整数
函数种类: 网络系统
内容说明: 本函数用来释放返回资料所使用的内存。若没有使用本函数释放内存,程序结束时也会自动释放。

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个属性值
使用范例
下面范例为片段的程序
<?php
$entry = ldap_first_entry($ds, $sr);
$attrs = ldap_get_attributes($ds, $entry);
echo $attrs["count"]." 笔属性资料如下:<p>n";
for ($i=0; $i<$attrs["count"]; $i++) {
echo $attrs[$i]."<br>";
}
?>
参考: ldap_first_attribute() ldap_next_attribute()

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
取得全部返回资料。
语法: 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
取得全部返回值。
语法: 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"]." 笔电子邮件地址: <p>n";
for ($i=0; $i < $values["count"]; $i++) {
echo $values[$i]."<br>";
}
?>

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 可省略,用来配置更细的列出属性。
使用范例
这个范例为片段程序
<?php
$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 名录的属性。
语法: boolean ldap_modify(int handle, string dn, array entry);
返回值: 布尔值
函数种类: 网络系统
内容说明: 本函数用来改变 LDAP 服务器上目前所在名录上的属性。参数 handle 为打开 LDAP 的代号。参数 dn 为要加入条目的具体 dn 字符串。参数 entry 为数组,为个体所有的属性,数组的内容是名录属性的相关信息。若无错误则返回 true 值。
使用范例
本例为 nickt@powys.gov.uk 提出的片段 (23-Apr-1999)
<?php
$newinfo[mail]="nick@county.gov.uk";
ldap_modify($valid_ldaplink, $valid_dn, $newinfo);
?>
参考: ldap_mod_add()

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
取得下一笔结果代号。
语法: 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
取得目前的资料属性。
语法: 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
列出树状简表。
语法: 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 可省略,用来配置更细的列出属性。
使用范例
下面范例为片段程序
<?php
$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<p>";
?>
参考: ldap_list() ldap_read()

ldap_unbind
结束 LDAP 链接。
语法: boolean ldap_unbind(int handle);
返回值: 布尔值
函数种类: 网络系统
内容说明: 本函数将关闭与 LDAP 服务器的链接。参数 handle 为打开 LDAP 的代号。若无错误则返回 true 值。
参考: ldap_close()

 
 
  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·用Php如何操作LDAP  (2005-03-11)

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·Windows下集成安装Apache,PHP,MYSQ
·Mysql注入:SQL Injection with MyS
·PHP 的来龙去脉
·PHP 的功能概述
·PHP与其它CGI的比较
·PHP 的编译配置详细选项
·php.ini 配置详细选项
·如何写作PHP程序
·Hello,World
·嵌入方法
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统