title: PHP LDAP Get User Attributes, Including Associated Groups
body: <p>What is the best way to run a search on the current user to retrieve all attributes, including associated groups in Active Directory using LDAP / PHP?</p>
  
  <p>For attributes, mainly just first name, last name, and display name.</p>
  
  <p>For associated groups, just the groups the user is a member of, such as the memberOf function.</p>
  
  <p>I've tried a few options, but can't seem to get the right filter / search combination and most examples cover retrieving lists of users where there's a known group.</p>
  
  <p>I've tried running this after a successful bind:</p>
  
  <pre><code>$attributes = array("displayname");
  $filter = "(&amp;(sAMAccountName=$username))";
  $result = ldap_search($ds, $ldapconfig['basedn'], $filter, $attributes);
  $entries = ldap_get_entries($ds, $result);
  if($entries["count"] &gt; 0){
    echo "displayName: ".$entries[0]['displayname'][0]."&lt;br/&gt;";
   } else {
   echo("msg:'".ldap_error($ds)."'&lt;/br&gt;");
   }
  </code></pre>
  
  <p>Which returns the following error: "No such object".</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>This is the latest block I've tried and am able to get results when I print_r the $info variable, however the for clause is still erring out somewhere. I changed the basedn to just the dc attributes:</p>
  
  <pre><code>$filter="($SearchField=$SearchFor)";
  $sr=ldap_search($ds, $basedn, $filter, $LDAPFieldsToFind);
  $info = ldap_get_entries($ds, $sr);
  
  if($info["count"] &gt; 0) {
      for ($x=0; $x&lt;$info["count"]; $x++) {
          $sam=$info[$x]['samaccountname'][0];
          $giv=$info[$x]['givenname'][0];
          $tel=$info[$x]['telephonenumber'][0];
          $email=$info[$x]['mail'][0];
          $nam=$info[$x]['cn'][0];
          $dir=$info[$x]['homedirectory'][0];
          $dir=strtolower($dir);
          $pos=strpos($dir,"home");
          $pos=$pos+5;
              if (stristr($sam, $SearchFor) &amp;&amp; (strlen($dir) &gt; 8)) {
                print "\nActive Directory says that:\n";
                print "CN is: ".$nam." \n";
                print "SAMAccountName is: ".$sam." \n";
                print "Given Name is: ".$giv." \n";
                print "Telephone is: ".$tel." \n";
                print "Home Directory is: ".$dir." \n";
              }   
      }
      }
  </code></pre>
  
  <p>The print_r of the results are:</p>
  
  <pre><code>( [count] =&gt; 1 [0] =&gt; Array ( [cn] =&gt; Array ( [count] =&gt; 1 [0] =&gt; George ) [0] =&gt; cn [givenname] =&gt; Array ( [count] =&gt; 1 [0] =&gt; George ) [1] =&gt; givenname [memberof] =&gt; Array ( [count] =&gt; 4 [0] =&gt; CN=EQCStaff,CN=Users,DC=EQC,DC=local [1] =&gt; CN=RDS Users,OU=Security Groups,OU=Service,DC=EQC,DC=local [2] =&gt; CN=SFTP Client Folders,OU=Security Groups,OU=Service,DC=EQC,DC=local [3] =&gt; CN=EQC Staff,OU=Security Groups,OU=Service,DC=EQC,DC=local ) [2] =&gt; memberof [samaccountname] =&gt; Array ( [count] =&gt; 1 [0] =&gt; gortiz ) [3] =&gt; samaccountname [mail] =&gt; Array ( [count] =&gt; 1 [0] =&gt; user@domain.com ) [4] =&gt; mail [count] =&gt; 5 [dn] =&gt; CN=George,OU=Users,OU=Accounts,DC=EQC,DC=local ) )
  </code></pre>