title: How to retrieve user info fra a Active Directory Security Group using LDAP and PHP
body: <p>As you can see below I'm not getting any user info when I do a LDAP search to the security group. I want to use the <code>$_SERVER[remote_user]</code> to check if the user is a member of this group. I would also like to retrieve the info of this user and update the sql database with it. Is this possible? </p>
  
  <pre><code>$dn = "CN=Intra,OU=Common Security Groups,DC=mydomain,DC=local";
  $filter = "(member=*)";
  
  $ad = ldap_connect("IP") or die("Couldn't connect to AD!");
  ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3);
  $bd = ldap_bind( $ad, "username@mydomain.local", "password") or die("Can't bind to server.");
  $sr = ldap_search($ad,$dn,$filter);
  $entries = ldap_get_entries($ad, $sr);
  </code></pre>
  
  <p><code>print_r($entries);</code> </p>
  
  <p>Returns this:</p>
  
  <pre><code>Array
  (
      [count] =&gt; 1
      [0] =&gt; Array
          (
              [objectclass] =&gt; Array
                  (
                      [count] =&gt; 2
                      [0] =&gt; top
                      [1] =&gt; group
                  )
  
              [0] =&gt; objectclass
              [cn] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; Intra
                  )
  
              [1] =&gt; cn
              [description] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; Group for (LDAP) INTRANET server access
                  )
  
              [2] =&gt; description
              [member] =&gt; Array
                  (
                      [count] =&gt; 4
                      [0] =&gt; CN=Fname1 Lname1,OU=Mail enabled users,OU=Aberdeen,DC=mydomain,DC=local
                      [1] =&gt; CN=Fname2 Lname2,OU=Mail enabled users,OU=Forres,DC=mydomain,DC=local
                      [2] =&gt; CN=Fname3 Lname3,OU=Houston,DC=mydomain,DC=local
                      [3] =&gt; CN=Fname4 Lname4,OU=Mail enabled users,OU=Bergen,DC=mydomain,DC=local
                  )
  
              [3] =&gt; member
              [distinguishedname] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; CN=Intra,OU=Common Security Groups,DC=mydomain,DC=local
                  )
  
              [4] =&gt; distinguishedname
              [instancetype] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; 4
                  )
  
              [5] =&gt; instancetype
              [whencreated] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; 20100711172407.0Z
                  )
  
              [6] =&gt; whencreated
              [whenchanged] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; 20100712063949.0Z
                  )
  
              [7] =&gt; whenchanged
              [usncreated] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; 17491499
                  )
  
              [8] =&gt; usncreated
              [usnchanged] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; 17498823
                  )
  
              [9] =&gt; usnchanged
              [name] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; Intra
                  )
  
              [10] =&gt; name
              [objectguid] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt;
                  )
  
              [11] =&gt; objectguid
              [objectsid] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt;
                  )
  
              [12] =&gt; objectsid
              [samaccountname] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; Intra
                  )
  
              [13] =&gt; samaccountname
              [samaccounttype] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; 268435456
                  )
  
              [14] =&gt; samaccounttype
              [grouptype] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; -2147483646
                  )
  
              [15] =&gt; grouptype
              [objectcategory] =&gt; Array
                  (
                      [count] =&gt; 1
                      [0] =&gt; CN=Group,CN=Schema,CN=Configuration,DC=mydomain,DC=local
                  )
  
              [16] =&gt; objectcategory
              [count] =&gt; 17
              [dn] =&gt; CN=Intra,OU=Common Security Groups,DC=mydomain,DC=local
          )
  
  )
  </code></pre>
  
  <p>Everything worked fine when I used the normal DN:</p>
  
  <pre><code>$dn = "OU=Mail enabled users,OU=Bergen,DC=mydomain,DC=local";
  </code></pre>
  
  <p>But a AD expert told me this was a big NO-NO and that I should use Security Groups instead :\</p>