Skip to content
PHP Logo

Are there any specific PHP functions or libraries recommended for LDAP queries in this context?

When working with LDAP queries in PHP, the recommended approach is to use the `ldap_search()` function along with other LDAP functions provided by PHP's LDAP extension. This allows you to perform searches on the LDAP directory and retrieve the desired information. Additionally, using the `ldap_bind()` function to authenticate with the LDAP server is essential before performing any queries.

php
// LDAP connection settings
$ldapServer = 'ldap.example.com';
$ldapPort = 389;
$ldapBaseDn = 'dc=example,dc=com';
$ldapAdmin = 'cn=admin,dc=example,dc=com';
$ldapPassword = 'admin_password';

// Connect to LDAP server
$ldapConn = ldap_connect($ldapServer, $ldapPort);
ldap_set_option($ldapConn, LDAP_OPT_PROTOCOL_VERSION, 3);

// Bind to LDAP server
ldap_bind($ldapConn, $ldapAdmin, $ldapPassword);

// Search for users in LDAP directory
$searchFilter = '(objectClass=user)';
$searchResults = ldap_search($ldapConn, $ldapBaseDn, $searchFilter);
$entries = ldap_get_entries($ldapConn, $searchResults);

// Loop through search results
foreach ($entries as $entry) {
    // Process each user entry
    // Example: echo $entry['cn'][0];
}

// Close LDAP connection
ldap_close($ldapConn);