Search code examples
phpactive-directoryserverldapradix

PHP LDAP Connection


I was sent the following LDAP parameters, but am not sure how to establish a connection in PHP. I'm not sure which PHP function to use with each set of parameters. Here are the parameters I was given:

Server: ldaps://the_server.com:636

root DN: dc=the_info,dc=more_info,dc=com

User search base: ou=CompanyUsers

User search filter: sAMAccountName={0}

Group search base: OU=Security,OU=CompanyGroups

Group search filter: cn={0}

Group membership: Group membership attribute = memberOf

Display Name LDAP attribute: displayname

Email Address LDAP atribute: mail

If someone could provide a php script for me that would be great! This is my first time using LDAP and still do not understand all these parameters.


Solution

  • Following is the working code for linux base ldap. It might be helpful to you.

    <?php
    $username = 'uid=amitkawasthi,ou=CompanyUsers,dc=the_info,dc=more_info,dc=com'; 
    $password= 'test'; 
    $ds=ldap_connect("the_server.com, 636"); 
    echo $ds;
    if ($ds) { 
       echo "Binding ..."; 
       $r=ldap_bind($ds, $username, $password); 
       if ($r)
       { 
       $sr=ldap_search($ds,"ou=CompanyUsers,dc=the_info,dc=more_info,dc=com", "uid=amitkawasthi");   
       $entry = ldap_first_entry($ds, $sr); 
       $attrs = array(); 
       $attribute = ldap_first_attribute($ds,$entry,$identifier); 
       while ($attribute) { 
         $attrs[] = $attribute; 
         $attribute=ldap_next_attribute($ds,$entry,$identifier); 
         } 
        echo count($attrs) . " attributes held for this entry:<p>"; 
    
        $ldapResults = ldap_get_entries($ds, $sr); 
    //for ($item = 0; $item < $ldapResults['count']; $item++) { 
      // for ($attribute = 0; $attribute < $ldapResults[$item]['count'];                  $attribute++) { 
        //echo  $data = $ldapResults[$item][$attribute]; 
        echo  $data = $ldapResults[0][$attribute]; 
        echo $data.":&nbsp;&nbsp;".$ldapResults[0][$data][0]."<br>";             
       //} 
    ///echo '<hr />'; 
    
       echo "OK"; 
       }
       else
       {
       echo "Fail"; 
    
       }
    } 
    
    ?>
    ============================