【发布时间】:2014-05-23 12:34:53
【问题描述】:
我正在尝试使用我的 ldap 搜索,它有点工作,但是每个搜索查询需要大约 10-20 秒才能给我返回结果。虽然我必须在我的活动目录中处理大约 50.000 个条目(用户),你会说难怪它需要这么多时间,因为 ldap_search 是 O(N),我不相信它真的需要这么多时间。
我有一个可以输入姓名的文本框。在第三个键入的字母之后,搜索函数触发并将 textbox.value 作为参数传递给下面的 PHP 文件(通过 AJAX)。
<?php
error_reporting(E_ERROR | E_PARSE);
if(filter_input_array(INPUT_POST))
{
$term = filter_input(INPUT_POST, 'term');
$username = 'username';
$password = "password";
$ldap_host = array('host1', 'host2', 'host3');
$ldap_base_dn = "baseDN";
foreach ($ldap_host as $host)
{
$connect = ldap_connect($host);
if($connect)
{
break;
}
if(!$connect && $host == end($ldap_host))
{
exit(">> Could not connect to any of the given LDAP servers. <<");
}
}
ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
ldap_set_option($connect, LDAP_OPT_SIZELIMIT, 20);
$bind = ldap_bind($connect, $username, $password);
$search_filter = '(&(objectClass=person)(cn=*'.$term.'*))';
$attributes = array();
$attributes[] = 'givenname';
$attributes[] = 'mail';
$attributes[] = 'samaccountname';
$attributes[] = 'sn';
$attributes[] = 'cn';
$result = ldap_search($connect, $ldap_base_dn, $search_filter, $attributes);
$ArrayOfHumanoids = array();
if (FALSE !== $result)
{
$entries = ldap_get_entries($connect, $result);
for ($i = 0; $i < $entries['count']; $i++)
{
if (!empty($entries[$i]['givenname'][0]) &&
!empty($entries[$i]['mail'][0]) &&
!empty($entries[$i]['samaccountname'][0]) &&
!empty($entries[$i]['sn'][0]) &&
'Shop' !== $entries[$i]['sn'][0] &&
'Account' !== $entries[$i]['sn'][0])
{
$ad_users[strtoupper(trim($entries[$i]['samaccountname'][0]))] = array('email' => strtolower(trim
($entries[$i]['mail'][0])), 'first_name' => trim($entries[$i]['givenname'][0]), 'last_name' => trim($entries[$i]
['sn'][0]));
array_push($ArrayOfHumanoids, $entries[$i]['cn'][0] . "+");
}
}
}
if(count($ArrayOfHumanoids) == 0)
{
echo "<div>Sorry, no match found!<br></div>";
}
else
{
foreach($ArrayOfHumanoids as $userVar)
{
echo $userVar;
}
}
ldap_unbind($connect);
}
?>
$term 是我在每次击键时传递的搜索参数。在 AJAX 回调函数中,如您所见,我返回 ArrayOfHumanoids,json 回调获取并按 + 字符串拆分。我对那部分没有问题。我只是不明白为什么它这么慢。我是 LDAP 的新手,或者是递归的。提前谢谢你,我很感激任何提示!
【问题讨论】:
-
是搜索还是连接时间长?
-
我也想知道。我猜是搜索,但我不能真正将 2 分开,因为如果我想搜索,我必须先连接。
-
不要用ajax,直接访问页面,连接前后用echo,看看是不是这个问题?同时设置 ldap 调试选项以获取更多信息:
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7); -
如果没有 AJAX,我应该如何使用它?我的意思是,应该在每次击键时发送文本框的值,我只能通过使用 js onkey* 事件来做到这一点。而且我不能用它调用 SearchNames() 函数。
-
在您的网络检查器中打开您的网络选项卡,找到 ajax 调用,右键单击,选择“在新窗口中打开链接”