Skip to content
PHP Logo

What are best practices for querying and modifying LDAP entries in PHP, especially when dealing with referrals?

When querying and modifying LDAP entries in PHP, especially when dealing with referrals, it is important to handle referrals appropriately to ensure the operations are successful. One way to achieve this is by enabling referral chasing in the LDAP connection settings. This allows PHP to automatically follow referrals when querying or modifying LDAP entries.

php
// Enable referral chasing in LDAP connection settings
ldap_set_option(null, LDAP_OPT_REFERRALS, 0);

// Connect to LDAP server
$ldapconn = ldap_connect("ldap://example.com");

// Bind to LDAP server
ldap_bind($ldapconn, "cn=admin,dc=example,dc=com", "password");

// Perform LDAP query or modification operations
// For example, to search for an entry
$result = ldap_search($ldapconn, "dc=example,dc=com", "(&(objectClass=person)(uid=johndoe))");

// Handle search result
if ($result) {
    $entries = ldap_get_entries($ldapconn, $result);
    // Process entries
}

// Close LDAP connection
ldap_close($ldapconn);