Changing LDAP Query Size Limit More Than 1000 in PHP

Fix the PHP ldap_search() 'Sizelimit exceeded' warning against Active Directory: page results in code, or raise MaxPageSize with ntdsutil safely.

Changing LDAP Query Size Limit More Than 1000 in PHP

If your PHP application searches Active Directory and more than 1,000 objects match, you hit the directory's LDAP query size limit. You will see this warning: Warning: ldap_search(): Partial search results returned: Sizelimit exceeded. Your code receives the first 1,000 entries and misses the rest unless you handle it.

In 2021 I fixed this by raising the LDAP query size limit on the domain controller. That method still works, and I keep it below. But the better fix today is in the PHP code itself, so I start there.

Why the LDAP query size limit is 1,000

Active Directory has a setting called MaxPageSize, which controls how many objects a single LDAP search can return. The default is still 1,000, and it applies however large or small each object is.

The setting lives in the Default Query Policy, which applies across the directory, not on one server. Microsoft's guidance is clear: a client that expects more results than the limit must use the paged search control (Microsoft Learn: view and set LDAP policy using ntdsutil). In other words, the server limit is working as intended; the client should ask for results page by page.

The better fix: page the results in PHP

Older tutorials use ldap_control_paged_result() and ldap_control_paged_result_response(). These were deprecated in PHP 7.4 and removed in PHP 8.0. The PHP manual now points to the controls parameter of ldap_search() instead (PHP manual: ldap_control_paged_result).

With paging, the server limit can stay at 1,000 and you still get every object:

// Example only: paged search against Active Directory (PHP 7.3+)
$ldap = ldap_connect('ldaps://dc1.example.local');
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);

if (!ldap_bind($ldap, getenv('LDAP_BIND_USER'), getenv('LDAP_BIND_PASSWORD'))) {
    throw new RuntimeException(ldap_error($ldap));
}

$cookie = '';
do {
    $result = ldap_search($ldap, $baseDn, $filter, ['cn', 'mail'], 0, -1, -1, LDAP_DEREF_NEVER,
        [['oid' => LDAP_CONTROL_PAGEDRESULTS, 'value' => ['size' => 500, 'cookie' => $cookie]]]);
    if ($result === false) {
        throw new RuntimeException(ldap_error($ldap));
    }
    ldap_parse_result($ldap, $result, $errcode, $matcheddn, $errmsg, $referrals, $controls);
    $entries = ldap_get_entries($ldap, $result);
    // process $entries ... ($entries['count'] is the number on this page)
    $cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? '';
} while (!empty($cookie));

A page size of 500 keeps each response well under the limit. The loop ends when the server stops returning a cookie. If you use a framework or LDAP library rather than the raw functions, check that your version pages through server controls on PHP 8, and run a supported PHP version (PHP supported versions).

The original fix: raise MaxPageSize with ntdsutil

If you cannot change the application, you can raise the limit on the directory. Use a modest value, and read the cautions first.

  1. Open Command Prompt on a domain controller with administrator credentials.
  2. Type ntdsutil and press Enter.
  3. At the ntdsutil: prompt, type ldap policies.
  4. At the ldap policy: prompt, type connections.
  5. At the server connections: prompt, type connect to server followed by the FQDN of your domain controller, for example connect to server dc1.example.local.
  6. Once connected, type q to return to the ldap policy: prompt.
  7. To allow up to 5,000 objects per query, type set MaxPageSize to 5000.
  8. Type show values to review the change before committing it.
  9. Type commit changes, then q twice to exit.

The full session looks like this:

ntdsutil
ldap policies
connections
connect to server dc1.example.local
q
set MaxPageSize to 5000
show values
commit changes
q
q

In my original note, show values came after the commit. I now run it before, because at that point it shows the current and proposed values side by side, such as 1000(5000). You can spot a typo before it goes live.

How to check it worked

  • On the server: run ntdsutil, go back into ldap policies, connect, and type show values. MaxPageSize should show the new value with no pending change in brackets.
  • In PHP: run your search again and compare the total number of entries with what you expect. The Sizelimit exceeded warning should be gone.
  • With paging: log the count from each page. You should see several pages of 500 and one smaller final page.

Cautions before you change anything

  • It is directory-wide. The change is written to the Default Query Policy, which affects every client. Note too that ntdsutil only shows the default policy, so any custom query policies will not appear.
  • Record the original value. Write down the output of show values before you change anything, and follow your change-control process. Make sure you have a current backup of your domain controllers before modifying directory settings.
  • Do not keep raising it. Higher limits mean larger single responses and more load on domain controllers. If 5,000 is not enough, that is a sign to implement paging.
  • Large groups are a separate issue. A big group's member attribute is governed by a different range limit, so raising MaxPageSize will not return all members. That needs ranged retrieval in the client.
  • Encrypt the connection. Avoid binding over plain, unencrypted LDAP on port 389. Use LDAPS (port 636) or StartTLS, as in the example above, so credentials and directory data are not sent in clear text.

Quick decision table

Situation What I recommend
You control the PHP code Page results with the controls parameter; leave MaxPageSize at 1,000
Legacy app you cannot change Raise MaxPageSize modestly with ntdsutil, after a backup
Code uses ldap_control_paged_result() Rewrite it; it does not exist in PHP 8.0 and later
Missing members of a large group Use ranged retrieval; MaxPageSize will not help

The ntdsutil change got me out of trouble in 2021, and it still works. But the lasting fix is to make your PHP code ask for results in pages. It scales, it leaves the directory at its defaults, and it keeps working as your directory grows.

Sources & further reading

  1. Microsoft Learn: View and set LDAP policy in Active Directory using Ntdsutil.exelearn.microsoft.com
  2. Microsoft Learn: ldap policies (ntdsutil reference)learn.microsoft.com
  3. PHP manual: ldap_control_paged_resultphp.net
  4. PHP manual: ldap_searchphp.net
  5. PHP manual: LDAP Controlsphp.net
  6. PHP: Supported Versionsphp.net

📝 First written from my own work in 2021; updated and expanded in November 2026 with AI assistance, keeping my original fix. Spotted something wrong? Tell me.

Akam Omer
Written by

Akam Omer

IT leader, engineer and security specialist from Erbil with 20+ years across higher education, telecoms, healthcare and government. Founder of BulkSMSIraq, VerifyWay and Kurd.ai.