PHP Function LDAP_Count_Entries
Article about php function ldap_count_entries
In this article I will be writing an example of using the LDAP API in PHP. LDAP is a directory service which uses a tree-based structure to store information. It's used by a number of different applications such as Microsoft Active Directory, OpenLDAP, Sun Directory Server and others. LDAP stands for Lightweight Directory Access Protocol, and the LDAP format is called LDIF (LDAP Data Interchange Format).
LDAP can be quite complex and sometimes it can be difficult to understand how to use it. The example below shows how to query for group members and then filter on user attributes based on the groups selected.
The LDAP API functions allow you to do all kinds of cool stuff, such as querying for a specific entry and finding out what attributes it has and so on. There are also some more advanced functions that allow you to do things such as sorting entries by a certain attribute.
This is a simple snippet I wrote for doing this. It's basically just a wrapper around ldap_get_entries() but it adds an extra parameter to return the entries sorted by last name.
This snippet was written because I kept running into problems trying to sort the LDAP results based on a GUID value returned by ldap_get_values_len(). It turns out that you need to convert the string GUID back into hex for it to work properly. This snippet will convert the GUID from binary to hex and then use sprintf to format it into a usable string that you can search AD for.