文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>使用PHP连接LDAP服务器(续)

使用PHP连接LDAP服务器(续)

时间:2007-02-17  来源:PHP爱好者

在我们的例子中,$format_array是我们建立的新数组,里面包括有查询的结果,并且被格式化用作输出。首先循环$result_array中的每个元素,并且将它分配给一个两维的数组用作排序。同时我们使用strtolower()函数将所有的值变为小写。
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
  接着,我们使用PHP自带的一个称为sort()的函数进行排序。首个参数是要排序的数组,另一个是要执行的排序类型,该类型是由PHP的文档定义的。由于我们根据字符串排序,我们使用“SORT_STRING”。
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
  第三,我们循环已经格式化好的数组,并且将它分配给一个名字为$result_list的输出字符,该字符包含了HTML描述。要特别注意的是,在超链接中,我使用的是ldap的URL格式。这个格式的例子类似:HREF="ldap://ldap.domain.net/uid=dannie,dc=domain.net"。
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
关闭连接
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
  现在我们所有的数据已经包含在$result_list中了,我们可以安全地关闭LDAP的连接。
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
<?php 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//关闭连接
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
ldap_close($connect_id); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
定制搜索界面的HTML表格
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
  最后,我们要定制搜索用的HTML表格,它是用来给用户执行搜索的。
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//定制表格
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo " <CENTER><FORM action='"$PHP_SELF"' method='"GET"'>"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
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>
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "Search for:<INPUT name='"common"' type='"text"'>"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "<INPUT name='"lookup"' type='"submit"' value='"go"'>
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "(You can use * for wildcard searches, ex. * Stanley will find all Stanleys)
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "</FORM></CENTER>"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
?> 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
  代码中的$PHP_SELF是一个全局的常量,代表的是脚本页面自身,其中的循环是用来通过我们的$LDAP_NAME变量创建SELECT选项。
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
显示结果
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
  现在所有的工作已经完成了,我们将打印出结果集。如果没有符合的结果,将会显示"No Results"的信息。
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
<<?>php 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//显示结果
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
if($result_list) 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo " <CENTER><TABLE border='"1"' cellPadding='"10"' cellSpacing='"0"' 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
BGCOLOR="#FFFFEA" WIDTH="450"> <TBODY><TR><TD>$result_list</TD></TR>
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
</TBODY></TABLE></CENTER>"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
else 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "No Results"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
?> 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
源代码
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
  以下是完整的源代码,只要将它剪切并粘贴到一个HTML文档,就可以尝试一下了。
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
<?php 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$LDAP_NAME[0] = "Netscape Net Center"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$LDAP_SERVER[0] = "memberdir.netscape.com"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$LDAP_ROOT_DN[0] = "ou=member_directory,o=netcenter.com"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$LDAP_NAME[1] = "Bigfoot"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$LDAP_SERVER[1] = "ldap.bigfoot.com"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$LDAP_ROOT_DN[1] = ""; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//如果没有选择服务器的话将它设置为0 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
if(!$SERVER_ID) 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$SERVER_ID=0; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//建立查询
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$ldap_query = "cn=$common"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//连接到LDAP 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$connect_id = ldap_connect($LDAP_SERVER[$SERVER_ID]); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
if($connect_id) 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//认证 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$bind_id = ldap_bind($connect_id); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//执行搜索 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$search_id = ldap_search($connect_id, $LDAP_ROOT_DN[$SERVER_ID], $ldap_query); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//将结果集合分配给一个数组 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$result_array = ldap_get_entries($connect_id, $search_id); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
else 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//显示连接错误 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "Could not connect to LDAP server: $LDAP_SERVER[$SERVER_ID]"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//如果搜索成功,将结果排序 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
if($result_array) 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
for($i=0; $i { 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$format_array[$i][0] = strtolower($result_array[$i]["cn"][0]); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$format_array[$i][1] = $result_array[$i]["dn"]; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$format_array[$i][2] = strtolower($result_array[$i]["givenname"][0]); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$format_array[$i][3] = strtolower($result_array[$i]["sn"][0]); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$format_array[$i][4] = strtolower($result_array[$i]["mail"][0]); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//排序数组 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
sort($format_array, "SORT_STRING"); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
for($i=0; $i { 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$cn = $format_array[$i][0]; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$dn = $format_array[$i][1]; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$fname = ucwords($format_array[$i][2]); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$lname = ucwords($format_array[$i][3]); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$email = $format_array[$i][4]; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
if($dn && $fname && $lname && $email) 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$result_list .= "<A href='/"ldap://$LDAP_SERVER[$SERVER_ID]/$dn/"'>$fname $lname</A>"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$result_list .= " <$email>
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
elseif($dn && $cn && $email) 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$result_list .= "<A href='/"ldap://$LDAP_SERVER[$SERVER_ID]/$dn/"'>$cn</A>"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
$result_list .= " <<A href='/"mailto:$email/"'>$email</A>
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
else 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "Result set empty for query: $ldap_query"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//关闭连接
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
ldap_close($connect_id); 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//定制表格
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo " <CENTER><FORM action='"$PHP_SELF"' method='"GET"'>"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "Search in:<SELECT name='"SERVER_ID"'>"; //循环以建立SELECT选项 for($i=0; $i echo "<OPTION selected value='"$i"'>".$LDAP_NAME[$i]."</OPTION>"; echo "</SELECT>
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "Search for:<INPUT name='"common"' type='"text"'>"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "<INPUT name='"lookup"' type='"submit"' value='"go"'>
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "(You can use * for wildcard searches, ex. * Stanley will find all Stanleys)
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "</FORM></CENTER>"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
//显示结果
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
if($result_list) 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo " <CENTER><TABLE border='"1"' cellPadding='"10"' cellSpacing='"0"' 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
BGCOLOR="#FFFFEA" WIDTH="450"> <TBODY><TR><TD>$result_list</TD></TR>
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
</TBODY></TABLE></CENTER>"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
else 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
echo "No Results"; 
China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp

China It Power . Coml2bOYYb3nPV6cHCPk1q57saEp
?>
php爱好者站 http://www.phpfans.net PHP|MySQL|javascript|ajax|html.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载