php - 控制PHP ldap分页-结果数和页码

原文 标签 php pagination active-directory ldap

Controlling PHP ldap pagination - number of results and page number

Hi this has been going around a little but hopefully I can get some light shone on the problem. Basically I would like to do a search in my active directory using ldap and paginate the results.

I've seen code like this around but it doesn't make any sense to me

// $ds is a valid link identifier (see ldap_connect)
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);

$dn        = 'ou=example,dc=org';
$filter    = '(|(sn=Doe*)(givenname=John*))';
$justthese = array('ou', 'sn', 'givenname', 'mail');

// enable pagination with a page size of 100.
$pageSize = 100;

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

  $result  = ldap_search($ds, $dn, $filter, $justthese);
  $entries = ldap_get_entries($ds, $result);

  foreach ($entries as $e) {
    echo $e['dn'] . PHP_EOL;
  }

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

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

What does this actually do? What is the cookie all about?

I could replace

$result  = ldap_search($ds, $dn, $filter, $justthese); 

with

$result  = ldap_search($ds, $dn, $filter, $justthese,0,100);

Which would do with filter and get me the first 100 rows.

The question in hand is how do I control ldap pagination successfully. i.e giving the number of results to return and the page that I want them from.


Update

Right the code above, I've given it a run and what it does is run the search, several times - returning in each iteration a new page until the end. So it seems that the cookie has some control over that.

Answer

The cookie holds an internal pointer (a ressource) to the currently returned part of the result set. So for the server to know where to continue you have to provide the pointer back as parameter. ThaT is at least currently the case but there are thoughts of changing that. I'll have to do some digging to find the current status of that.

Your second idea of setting the number of returned items to 100 will return the same results over and over again as it sets the maximum number of values that the server will return for each request.

You will have to be carefull with sorting though! currently there is only ldap_sort (which is deprecated as of PHP 7) which sorts the resultset on clientside. But as the pagination is done on the serverside and there currently is no way of doing serversided sorting (we are working on that) the result will not be what you might expect.

翻译

大家好,这个问题已经解决了一些,但希望我能对这个问题有所了解。基本上,我想使用ldap在活动目录中进行搜索并分页结果。

我已经看过像这样的代码,但对我来说没有任何意义

// $ds is a valid link identifier (see ldap_connect)
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);

$dn        = 'ou=example,dc=org';
$filter    = '(|(sn=Doe*)(givenname=John*))';
$justthese = array('ou', 'sn', 'givenname', 'mail');

// enable pagination with a page size of 100.
$pageSize = 100;

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

  $result  = ldap_search($ds, $dn, $filter, $justthese);
  $entries = ldap_get_entries($ds, $result);

  foreach ($entries as $e) {
    echo $e['dn'] . PHP_EOL;
  }

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

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


这实际上是做什么的? Cookie到底是什么?

我可以取代

$result  = ldap_search($ds, $dn, $filter, $justthese); 




$result  = ldap_search($ds, $dn, $filter, $justthese,0,100);


这将与过滤器,并让我前100行。

目前的问题是如何成功控制ldap分页。即给出要返回的结果数和我想要它们的页面。



更新资料

在上面的代码右边,我给了它一次运行,然后运行了几次搜索-在每次迭代中返回一个新页面,直到结束。因此,cookie似乎对此具有一定的控制权。
最佳答案
Cookie拥有指向结果集当前返回部分的内部指针(资源)。因此,为了让服务器知道从何处继续,您必须提供返回的指针作为参数。 ThaT至少目前是这样,但有改变的想法。我将不得不进行一些挖掘以找到其当前状态。

将返回的项目数设置为100的第二个想法将一遍又一遍地返回相同的结果,因为它设置了服务器将为每个请求返回的最大值数。

不过,您必须谨慎分类!当前只有ldap_sort(PHP 7起已弃用)对客户端的结果集进行排序。但是由于分页是在服务器端完成的,并且目前无法进行服务器端排序(我们正在努力进行排序),因此结果将不会达到您的预期。
相关推荐

php - 带有PHPStorm 10的PHP7

php - 如何防止第二个正则表达式重新替换?

php - 如何在PHP的$ _SESSION中保存值并进行检索

php - 与特质的定义顺序和内容有关的“未找到特质错误”

php - 按费用和名称对数组进行排序

php - WAMP上的Mysqli,错误-连接尝试失败

php - laravel查询返回奇怪的顺序

php - Silverstripe 3.2-如何在不同选项卡的ModelAdmin中管理同一数据对象的不同列表

php - 在SQL或脚本中排除ID

php - 将多个查询字符串转换为php中的seo友好url