php - LDAP 搜索功能非常慢

标签 php ajax ldap

我正在尝试使用 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 或递归还很陌生。预先感谢您,我感谢任何类型的提示!

最佳答案

好的,我找到了导致问题的原因。我将主机更改为 IP,而不是实际的 url,现在确实更快了。感谢 Ohgodwhy 指出问题在于连接,而不是搜索。

关于php - LDAP 搜索功能非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23829422/

相关文章:

javascript - AWS Lambda 无法通过 AJAX 工作

active-directory - LDAP 从搜索中排除子 OU

authentication - 如何在 Spring Security 中使用 LDAP 身份验证创建 token

php - 1 个操作中的 3 个查询取决于每个查询

javascript - 为什么 TinyMCE 不将我的 html 转换为其所见即所得格式?

javascript - 如何使用ajax和javascript保存xml文件

javascript - 在 Rails 应用程序中使用 AJAX 更新模型字段而不使用表单

r - 如何使用 R 运行 ldap 查询?

php - 限制数组中的项目数

php - 直接将数组插入MySQL数据库