LDAP issue, ldap_bind invalid dn syntax

I know that my mistake is going to be something really simple but I have tried to find the problem and I do not see it, maybe you can help me….

I am trying to create a function with php, so I can be able to connect to LDAP and find the desired information.

My php code is the following:

$ldapconfig['host'] = "";
$ldapconfig['port'] = NULL;
$ldapconfig['basedn'] = "dc=example,dc=com";
$ldapconfig['binddn'] = "user";
$ldapconfig['bindpw'] = "password";

function ldap_authenticate($user, $pass) {
global $ldapconfig;
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7); 
if ($user != "" && $pass != "") {
    if(!ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)) {
        return NULL;
    ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
    ldap_bind( $ds, $ldapconfig['binddn'], $ldapconfig['bindpw']);
    $r = ldap_search( $ds, $ldapconfig['basedn'], 'sAMAccountName=' . $user);
    if ($r) {
        $result = ldap_get_entries( $ds, $r);
        if ($result[0]) {
            if (ldap_bind( $ds, $result[0]['dn'], $pass) ) {
                return $result[0]['mail'][0];
return NULL;

When I try to run the code it gives me the following mistake:
ldap_bind invalid DN syntax on line xxxx
and that line is the following:

ldap_bind( $ds, $ldapconfig['binddn'], $ldapconfig['bindpw']);

I appreciate your help…

Leave a Reply


Hire Me
Follow Me!
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.