title: Search users in more OUs in LDAP with PHP
body: <p>My task is ultimately to implement a single sign-on solution to login a user to my system, using the AD credentials stored in his work machine. I'm using the <a href="http://php.net/ref.ldap" rel="nofollow"><code>ldap_</code> functions in PHP</a>.</p>
  
  <p>The problem I have is that I have to check the credentials against more than one OU (around 30, for now), and I haven't found a way to obtain those OUs from the AD system, so I have a big array of OUs as a stop-gap measure. I obtained this list with ADexplorer. Is there a way to do this with PHP?</p>
  
  <p>My first language is not English, so I'm having Adriano translate this for me, and I'm also having problems understanding the documentation.</p>
  
  <p>At this stage, I'm basically copying this code from an example in <a href="http://php.net/manual/es/function.ldap-search.php" rel="nofollow">the ldap_search manpage</a>:</p>
  
  <pre><code>&lt;?php
  $ds=ldap_connect($ldapserver);
  
  // 42 OUs in our case
  $dn[]='OU=ABC,DC=xyz,DC=ac,DC=uk';
  $dn[]='OU=DEF,DC=xyz,DC=ac,DC=uk';
  // ...
  
  $totalDns = count($dn);
  for ($i = 0; $i &lt; $totalDns; $i++) {
      $id[] = $ds;
  }
  
  $filter = 'samaccountname='.$_POST['username'];
  
  $result = ldap_search($id,$dn,$filter);
  
  $search = false;
  
  foreach ($result as $value) {
      if(ldap_count_entries($ds,$value)&gt;0){
          $search = $value;
          break;
      }
  }
  
  if($search){
      $info = ldap_get_entries($ds, $search);
  }else{
      $info = 'No results found';
  }
  </code></pre>
  
  <p><strong>Update</strong> </p>
  
  <p>I tried  the solution suggested by Vladislav Ross, and, after a few seconds, the server spits this:</p>
  
  <pre><code>mod_fcgid: stderr: PHP Warning:  ldap_search() [&lt;a href='function.ldap-search'&gt;function.ldap-search&lt;/a&gt;]: Search: Can't contact LDAP server in ... on line 28
  </code></pre>
  
  <p>This does not happen if I prepend a specific OU to the same search. I.E. if I do</p>
  
  <pre><code>$sr = ldap_search(
       $ds,
       "OU=Usuarios,dc=test,dc=com",
       "ObjectClass=organizationalUnit",
       array("")
  );
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>$sr = ldap_search(
        $ds,
        "dc=test,dc=com",
        "ObjectClass=organizationalUnit",
        array("")
  );
  </code></pre>
  
  <p>I do get a correct result. I tried setting the timelimit and sizelimit parameters to 0, but with the same results. I'm now consulting with the server guys to see what their limits are.</p>