【问题标题】:PHP LDAP paginationPHP LDAP 分页
【发布时间】:2019-06-27 02:17:08
【问题描述】:

我正在构建一个 PHP 脚本,它将获取特定人员下的所有用户,基本上是直接报告,可能会变得非常大。

我设法让脚本适用于 1000 个条目。我找到了这个网站https://doc.bccnsoft.com/docs/php-docs-7-en/function.ldap-control-paged-result.html 并将它放在我的代码周围。问题是,我不知道如何让这个脚本运行超过 1000 个条目。我希望它返回 2000、5000、10,000 条记录,直到完成所要求的数据。

<?php
set_time_limit(60);

$executionStartTime = microtime(true);
//LDAP Directory Services Settings
define("LDAP_SERVER", "test.com");
define("LDAP_DN", "OU=Users,DC=test,DC=com");
define("USER", "test");
define("LDAP_DOMAIN", "@test.com");
define("LDAP_USER", "test");
define("LDAP_PASS", "password");

/* Connect to LDAP Server */
$ds = ldap_connect(LDAP_SERVER);

/* Bind to LDAP Server */
$user_fqdn = LDAP_USER.LDAP_DOMAIN;
$bind = @ldap_bind($ds, $user_fqdn, LDAP_PASS);
//@ldap_bind($ldapconn, $ldapuser."@test.com", $ldappass);

/* Options for searching Whole Domain */
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
//ldap_set_option($ds, LDAP_OPT_SIZELIMIT, 5);

/* Search Filter */ 
$filter = "(&(objectCategory=Person)(sAMAccountName=*)(manager:1.2.840.113556.1.4.1941:=cn=".USER.",OU=Users,DC=test,DC=com))";

$justthese = array('mail', 'employeeType', 'manager');
/* Search */
$pageSize = 1000;

 $cookie = '';
 do {
     ldap_control_paged_result($ds, $pageSize, true, $cookie);

     $result  = ldap_search($ds, LDAP_DN, $filter, $justthese);
     $entries = ldap_get_entries($ds, $result);
     echo "<pre>";
     var_dump($entries);   
    echo "</pre>";       

     ldap_control_paged_result_response($ds, $result, $cookie);

 } while($cookie !== null && $cookie != '');


$executionEndTime = microtime(true);
$seconds = $executionEndTime - $executionStartTime;

//Print it out
echo "This script took " . $seconds . " to execute.";

?>

基本上,我希望它在 cron 作业上运行,只需从 ldap 获取所有条目并将其转储到数据库中(我还没有进入 db 部分)。不仅有 1000 条记录。

【问题讨论】:

  • “我找到了这个网站” - 仅供参考:您可以访问 php.net 并直接阅读文档。

标签: php pagination active-directory ldap


【解决方案1】:

您需要绑定之前设置协议版本,尤其是使用AD:

$ds = ldap_connect(LDAP_SERVER);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);

$user_fqdn = LDAP_USER.LDAP_DOMAIN;
$bind = @ldap_bind($ds, $user_fqdn, LDAP_PASS);

您的代码在 do/while 循环中看起来不错。

【讨论】:

  • 哇...成功了。它用数据溢出了多个数组!谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-06
  • 2016-09-26
  • 2011-08-22
  • 2016-11-01
相关资源
最近更新 更多