LDAP介绍
2.创建一个LDAP查询
3.连接到LDAP服务器
4.如果连接成功,处理查询
5.格式化输出
6.关闭连接
7.设计搜索界面的HTML表格
8.显示结果
"LDAP_SERVER" = 新的LDAP项目的IP地址或者主机名
"LDAP_ROOT_DN" = 新的LDAP项目的根的辨识名
$LDAP_SERVER[0] = "memberdir.netscape.com";
$LDAP_ROOT_DN[0] = "ou=member_directory,o=netcenter.com";
$LDAP_SERVER[1] = "ldap.bigfoot.com";
$LDAP_ROOT_DN[1] = "";
if(!$SERVER_ID)
$SERVER_ID=0;
$connect_id = ldap_connect($LDAP_SERVER[$SERVER_ID]);
{
//认证
$bind_id = ldap_bind($connect_id);
$search_id = ldap_search($connect_id, $LDAP_ROOT_DN[$SERVER_ID], $ldap_query);
$result_array = ldap_get_entries($connect_id, $search_id);
}
else
{
//显示连接错误
echo "Could not connect to LDAP server: $LDAP_SERVER[$SERVER_ID]";
}
格式化输出
["dn"] [0] = "uid=dannie,dc=spinweb.net"
["givenname"][0] = "Dannie"
["sn"] [0] = "Stanley"
["mail"] [0] = "danSPAM@spinweb.net"
$result_array[1]["cn"] [0] = "Michael Reynolds"
["dn"] [0] = "uid=michael,dc=spinweb.net"
["givenname"][0] = "Michael"
["sn"] [0] = "Reynolds"
["mail"] [0] = "michaelSPAM@spinweb.net"
["dn"] [0] = "uid=dannie,dc=spinweb.net"
["givenname"][0] = "Dannie"
["givenname"][0] = "Dan"
["sn"] [0] = "Stanley"
["mail"] [0] = "danSPAM@spinweb.net"
"dn" = Distinguished Name
"givenname" = First Name
"sn" = Last Name
"mail" = Email地址
if($result_array)
{
for($i=0; $i {
$format_array[$i][0] = strtolower($result_array[$i]["cn"][0]);
$format_array[$i][1] = $result_array[$i]["dn"];
$format_array[$i][2] = strtolower($result_array[$i]["givenname"][0]);
$format_array[$i][3] = strtolower($result_array[$i]["sn"][0]);
$format_array[$i][4] = strtolower($result_array[$i]["mail"][0]);
}
sort($format_array, "SORT_STRING");
$cn = $format_array[$i][0];
$dn = $format_array[$i][1];
$fname = ucwords($format_array[$i][2]);
$lname = ucwords($format_array[$i][3]);
$email = $format_array[$i][4];
{
$result_list .= "$fname $lname";
$result_list .= " <$email>
";
}
elseif($dn && $cn && $email)
{
$result_list .= "<A href='/"ldap://$LDAP_SERVER[$SERVER_ID]/$dn/"'>$cn</A>";
$result_list .= " <A href='/"mailto:$email/"'>$email</A>
";
}
}
}
else
{
echo "Result set empty for query: $ldap_query";
}
在我们的例子中,$format_array是我们建立的新数组,里面包括有查询的结果,并且被格式化用作输出。首先循环$result_array中的每个元素,并且将它分配给一个两维的数组用作排序。同时我们使用strtolower()函数将所有的值变为小写。
ldap_close($connect_id);
echo " <CENTER><FORM action='"$PHP_SELF"' method='"GET"'>";
echo "Search in:<SELECT name='"SERVER_ID"'>"; //循环以建立SELECT选项 for($i=0; $i<COUNT($LDAP_NAME); <br $i++)> echo "<OPTION selected value='"$i"'>".$LDAP_NAME[$i]."</OPTION>"; echo "</SELECT>
";
echo "Search for:<INPUT name='"common"' type='"text"'>";
echo "<INPUT name='"lookup"' type='"submit"' value='"go"'>
";
echo "(You can use * for wildcard searches, ex. * Stanley will find all Stanleys)
";
echo "</FORM></CENTER>";
if($result_list)
{
echo " <CENTER><TABLE border='"1"' cellPadding='"10"' cellSpacing='"0"'
BGCOLOR="#FFFFEA" WIDTH="450"> <TBODY><TR><TD>$result_list</TD></TR>
</TBODY></TABLE></CENTER>";
}
else
echo "No Results";
$LDAP_SERVER[0] = "memberdir.netscape.com";
$LDAP_ROOT_DN[0] = "ou=member_directory,o=netcenter.com";
$LDAP_SERVER[1] = "ldap.bigfoot.com";
$LDAP_ROOT_DN[1] = "";
if(!$SERVER_ID)
$SERVER_ID=0;
$ldap_query = "cn=$common";
$connect_id = ldap_connect($LDAP_SERVER[$SERVER_ID]);
{
//认证
$bind_id = ldap_bind($connect_id);
$search_id = ldap_search($connect_id, $LDAP_ROOT_DN[$SERVER_ID], $ldap_query);
$result_array = ldap_get_entries($connect_id, $search_id);
}
else
{
//显示连接错误
echo "Could not connect to LDAP server: $LDAP_SERVER[$SERVER_ID]";
}
if($result_array)
{
for($i=0; $i {
$format_array[$i][0] = strtolower($result_array[$i]["cn"][0]);
$format_array[$i][1] = $result_array[$i]["dn"];
$format_array[$i][2] = strtolower($result_array[$i]["givenname"][0]);
$format_array[$i][3] = strtolower($result_array[$i]["sn"][0]);
$format_array[$i][4] = strtolower($result_array[$i]["mail"][0]);
}
sort($format_array, "SORT_STRING");
$cn = $format_array[$i][0];
$dn = $format_array[$i][1];
$fname = ucwords($format_array[$i][2]);
$lname = ucwords($format_array[$i][3]);
$email = $format_array[$i][4];
{
$result_list .= "<A href='/"ldap://$LDAP_SERVER[$SERVER_ID]/$dn/"'>$fname $lname</A>";
$result_list .= " <$email>
";
}
elseif($dn && $cn && $email)
{
$result_list .= "<A href='/"ldap://$LDAP_SERVER[$SERVER_ID]/$dn/"'>$cn</A>";
$result_list .= " <<A href='/"mailto:$email/"'>$email</A>
";
}
}
}
else
{
echo "Result set empty for query: $ldap_query";
}
ldap_close($connect_id);
echo " <CENTER><FORM action='"$PHP_SELF"' method='"GET"'>";
echo "Search in:<SELECT name='"SERVER_ID"'>"; //循环以建立SELECT选项 for($i=0; $i echo "<OPTION selected value='"$i"'>".$LDAP_NAME[$i]."</OPTION>"; echo "</SELECT>
";
echo "Search for:<INPUT name='"common"' type='"text"'>";
echo "<INPUT name='"lookup"' type='"submit"' value='"go"'>
";
echo "(You can use * for wildcard searches, ex. * Stanley will find all Stanleys)
";
echo "</FORM></CENTER>";
if($result_list)
{
echo " <CENTER><TABLE border='"1"' cellPadding='"10"' cellSpacing='"0"'
BGCOLOR="#FFFFEA" WIDTH="450"> <TBODY><TR><TD>$result_list</TD></TR>
</TBODY></TABLE></CENTER>";
}
else
echo "No Results";
}