How can one effectively handle different domain configurations, such as domain.local versus domain.privat.local, when working with LDAP in PHP?

When working with LDAP in PHP, one can effectively handle different domain configurations by using the LDAP base DN parameter to specify the appropriate domain. This allows the LDAP search to be targeted to a specific domain, whether it is domain.local or domain.privat.local.

// LDAP configuration for domain.local
$ldapServer = 'ldap://domain.local';
$ldapBaseDN = 'DC=domain,DC=local';

// LDAP configuration for domain.privat.local
$ldapServer = 'ldap://domain.privat.local';
$ldapBaseDN = 'DC=domain,DC=privat,DC=local';

// Connect to LDAP server
$ldapConn = ldap_connect($ldapServer);

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

// Search LDAP directory with specific base DN
$ldapSearch = ldap_search($ldapConn, $ldapBaseDN, $ldapFilter);

// Get LDAP search results
$ldapEntries = ldap_get_entries($ldapConn, $ldapSearch);

// Close LDAP connection
ldap_close($ldapConn);