How to get list of users from LDAP directory using PHP?

How to get list of users from LDAP directory using PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Troubleshooting LDAP Search Errors in PHP

When attempting to retrieve a list of users from an LDAP directory using PHP, the most common point of failure is the ldap_search() function returning an "Operations error." This specific warning usually indicates that the search criteria do not match any entries within the provided base distinguished name (DN), or that the connection lacks the necessary permissions to read those specific attributes.

The code snippet provided attempts to connect to my_host_name and search for a user with the filter (samaccountname=my_user_name). While the connection logic appears sound, the failure suggests a fundamental mismatch between the filter syntax and the actual directory structure or schema. In many Active Directory environments, the attribute name is case-sensitive regarding its definition but often case-insensitive in value matching depending on the server configuration. However, the most frequent cause of an "Operations error" in this context is attempting to search within a base DN that does not contain the object class user or where the scope is restricted.

To successfully retrieve a list of all users, you must adjust both the search filter and the base distinguished name. Simply passing my_dc_string as the base DN is often insufficient if it points to an organizational unit (OU) rather than the root domain context, or if the search scope needs adjustment. A robust approach involves setting the base DN to the root of your domain (e.g., DC=example,DC=com) and using a filter that targets the correct object class.

The standard filter for retrieving all user objects is (objectClass=user). If you are specifically looking for Active Directory users, you might also combine this with (samAccountType:1.2.840.113556.1.4.803:=805306368) to ensure you are only getting user objects and not computer accounts or groups that might share a similar naming convention. When searching for a list, remove the specific samaccountname from the filter entirely to avoid returning zero results if no single user matches that exact string.

Here is a corrected implementation pattern for retrieving a list of users:

$ldaphost = "ldap://my_host_name";
$basedn = 'DC=example,DC=com'; // Replace with your actual domain root
$samaccountname = ''; // Empty for a list search, or specific name if filtering one user
$filter = "(objectClass=user)";

// Optional: Add scope to limit depth if needed (0=subtree, 1=base, 2=onelevel)
$opt_ctrl = ldap_control_paged_result($ds, 500, 1); // Example of pagination control if supported

$result = ldap_search($ds, $basedn, $filter);

if ($result === false) {
    $errno = ldap_errno($ds);
    die("Search failed: " . ldap_error($ds));
}

// Iterate through results
while ($entry = ldap_first_entry($ds, $result)) {
    $userdn = ldap_get_dn($ds, $entry);
    // Fetch specific attributes like uidNumber or mail if required
    $attrs = ldap_get_attributes($ds, $entry, ['mail', 'cn']); 
}

A critical detail often overlooked is the handling of referrals. The original code explicitly disables referrals with LDAP_OPT_REFERRALS set to 0. While this is necessary for some setups (like when using a load balancer or proxy), it can prevent the search from completing if the directory server redirects requests to other domains for resolution. Ensure your initial connection string uses the correct protocol (ldap:// or ldaps://) and that port 389 (or 636 for LDAPS) is open on the host.

Another significant pitfall involves the scope of the search. If you are searching a nested structure, using LDAP_SCOPE_SUBTREE is usually required to traverse down into Organizational Units (OUs). If the base DN points directly to an OU and the scope is set to base or onelevel, users located in child OUs will be missed. The original snippet does not explicitly set the scope, which defaults to base on some PHP versions, effectively limiting the search to only the exact DN provided. For a comprehensive user list, you must ensure the scope covers the entire hierarchy.

Finally, always check for errors after every LDAP function call. Relying solely on checking if $result is truthy is insufficient because LDAP functions often return false with an errno even if memory allocation succeeded. Always inspect ldap_errno() and ldap_error() to distinguish between a network timeout, a protocol mismatch, or an actual search failure.

Our network