How can the page size for LDAP queries be adjusted in PHP to avoid the error message "Partial search results returned: Sizelimit exceeded" when querying an MS Active Directory server?

When querying an MS Active Directory server using LDAP in PHP, the error message "Partial search results returned: Sizelimit exceeded" may occur if the page size for LDAP queries is too small. To avoid this error, the page size can be adjusted in the LDAP query settings to allow for larger result sets to be returned.

// Connect to LDAP server
$ldapconn = ldap_connect("ldap://yourldapserver.com");

// Set LDAP options for paging
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);

// Set page size for LDAP queries
ldap_set_option($ldapconn, LDAP_OPT_SIZELIMIT, 1000); // Adjust the page size as needed

// Bind to LDAP server
$bind = ldap_bind($ldapconn, "username", "password");

// Perform LDAP search
$result = ldap_search($ldapconn, "ou=users,dc=example,dc=com", "(objectclass=*)");

// Get LDAP search results
$entries = ldap_get_entries($ldapconn, $result);

// Close LDAP connection
ldap_close($ldapconn);