How to PHP ldap_search() to get user OU if I don't know the OU for base DN

How to PHP ldap_search() to get user OU if I don't know the OU for base DN

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

Stefan Bogdanescu

Founder & Senior Architect

How to PHP ldap_search() to get user OU if I don't know the OU for base DN

One of the most common stumbling blocks when integrating PHP with Active Directory is constructing the correct Base Distinguished Name (Base DN). If you know the specific Organizational Unit (OU) where your users live, such as ou=it,dc=company,dc=co,dc=uk, the search works immediately. However, if user objects are scattered across multiple OUs—or if you don't know exactly where a specific user resides—passing a partial or incorrect DN to ldap_search() results in an "Operation Error."

The immediate instinct is often to use wildcards. While LDAP does support wildcard matching on certain attributes (like (&(objectClass=user)(sAMAccountName=j*))), relying on wildcards for the Base DN itself is not the standard approach and can lead to performance issues or inconsistent results depending on the server configuration.

The robust solution involves adjusting the client options between establishing the connection and performing the bind. Specifically, you must configure the LDAP library to handle referrals correctly and ensure it speaks the modern protocol version expected by most Active Directory servers.

Disabling Referrals and Setting Protocol Version

When connecting to an Active Directory server via PHP's ldap_* functions, two specific options are critical for searching without knowing the exact OU hierarchy:

  1. LDAP_OPT_REFERRALS (Set to 0): By default, LDAP clients follow referrals returned by the server. In an AD environment, this can sometimes cause issues if the server redirects you unexpectedly or if the referral logic interferes with anonymous binds during the initial search phase. Disabling this forces the client to stick to the connection provided.
  2. LDAP_OPT_PROTOCOL_VERSION (Set to 3): Active Directory primarily operates on LDAPv3. If your PHP extension defaults to an older version or a mismatch occurs, authentication and search operations will fail. Explicitly setting the protocol version ensures compatibility.

You should insert these two lines immediately after ldap_connect() and before attempting any bind or search operations. This configuration allows you to perform a "flat" search across the entire directory tree starting from the root domain context (e.g., dc=company,dc=co,dc=uk), effectively bypassing the need to know the specific OU path beforehand.

Implementation Example

Below is a structural example of how to integrate these options into your authentication flow. The key is placing the option setters between the connection and the bind call.

$ldap = ldap_connect($server); // Establish connection
if ($ldap) {
    ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
    
    $bind_result = ldap_bind($ldap, $username, $password);

    if ($bind_result) {
        // Now you can search the root DC without knowing the specific OU
        $filter = "(&(objectClass=user)(sAMAccountName=$username))";
        $result = ldap_search($ldap, $base_dn_root, $filter, array("dn", "cn", "memberof"));
        
        if ($result) {
            $entries = ldap_get_entries($ldap, $result);
            // Process entries...
        }
    }
}

Designing the Search Filter

Once the connection is stabilized with the correct options, your search filter becomes less restrictive regarding location. You can construct a filter that targets user objects globally within the domain context:

(&(objectClass=user)(objectCategory=person)(sAMAccountName=$username))

This filter ensures you are grabbing standard user accounts while ignoring computer objects or disabled accounts (which require adding !(userAccountControl:1.2.840.113556.1.4.803:=2) to the filter). This approach guarantees that even if a user is in ou=hr or ou=sales, the search will locate them as long as the Base DN points to the domain root.

A Note on OU Structure

Regarding your question about whether user objects should reside inside OUs at all: yes, they absolutely should. While it is possible to place users directly under a Users container or the root of the domain, organizing them into OUs (like IT, HR, Sales) is best practice for Active Directory management. It allows administrators to apply Group Policy Objects (GPOs) and security permissions at a granular level. For instance, you can force password changes or restrict access to specific printers for the entire "Sales" OU without touching every individual user object.

By configuring your PHP client to respect referrals and use protocol version 3, you gain the flexibility to authenticate users regardless of their specific physical location within the directory tree, making your application more resilient to structural changes in the AD environment.

Our network