I’m a LDAP newbie and I’m trying to get a PHP webapp to talk to my company’s Active Directory server.
I have the following code:
$ldap_server = 'ldap://server.domain.local';
$ldap_domain = '@domain.local';
$ldapport = 389;
$ldap_admin_user = "admin_user";
$ldap_admin_password = 'admin_password';
try {
$ldapconn = ldap_connect($ldap_server);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
$bind = ldap_bind($ldapconn, $ldap_admin_user.$ldap_domain, $ldap_admin_password);
if ($bind) {
$arr = array('cn', 'sAMAccountName');
$ldapsearch = (object)[];
$ldapsearch = ldap_search(
$ldapconn,
'OU=Users,DC=domain,DC=local',
"(objectClass=person)",
$arr
) or die ("Error trying to bind: ".ldap_error($ldapconn));
var_dump ($ldapsearch);
//$entries = ldap_get_entries($ldap, $ldapsearch);
}
else {
echo "SyncError: LDAP bind failed"
}
} catch (Exception $e) {
echo "SyncError: LDAP connection failed"
}
I get the following result:
object(LDAPResult)#24 (0) { }
However, if I use an LDAP explorer (JXplorer) on the same machine using the same credentials, protocol, and exact same filter string, then I can see the hierarchy of results. I have no idea why PHP is not grabbing the entries. I am on PHP 8.3.9 if that helps. This is also on a Windows machine.
I have tried changing up the filter string. Tried binding using a different admin user. Explicitly declaring the port in the ldap_connect. No change.
Trieu Nguyen-Tran is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1