<?php
//1、连接ladp
$ldaphost = "ldap.example.com"; // your ldap servers
$ldapport = 389; // your ldap server's port number
// Connecting to LDAP
$ds= ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost");
if ($ds) {
$r=ldap_bind($ds,"cn=root, o=My Company, c=US", "secret");
$info["cn"]="Roberto Celestino";
$info["sn"]="Roberto";
$info["mail"]="rcelestino@here.and.now";
$info["objectclass"]="person";
// add data to directory
$r=ldap_add($ds, "cn=Celestino Roberto, o=Mi Compania, c=US", $info);
//ldap_search\ldap_get_entries
$dn = "o=My Company, c=US";
$filter="(|(sn=$person*)(givenname=$person*))";
$justthese = array("ou", "sn", "givenname", "mail");
$sr=ldap_search($ds, $dn, $filter, $justthese);
$info = ldap_get_entries($ds, $sr);
//Close the link apointed by the identified $ds (get with ldap_connect)
ldap_close($ds);
}
LDAP目录记录的标识名(Distinguished Name,简称DN)是用来读取单个记录,以及回溯到树的顶部。
LDAP目录树的最顶部就是根,也就是所谓的“基准DN"。基准DN通常使用下面列出的三种格式:
o=foobar . com
(用公司的Internet地址表示的基准DN)
这种格式很直观,用公司的
域名作为基准DN。这也是现在最常用的格式。
dc=foobar, dc=com
(用DNS
域名的不同部分组成的基准DN)
就象上面那一种格式,这种格式也是以DNS
域名为基础的,但是上面那种格式不改变域名(也就更易读),而这种格式把域名:foobar点com分成两部分 dc=foobar, dc=com。
DN是LDAP记录项的名字
在LDAP目录中的所有记录项都有一个唯一的“Distinguished Name",也就是DN。每一个LDAP记录项的DN是由两个部分组成的:相对DN(RDN)和记录在LDAP目录中的位置。