Marco Aurélio Deleu
Marco Aurélio Deleu

Reputation: 4367

How to identify different errors on ldap_bind()? PHP

I'm using ldap_bind to connect to a Zimbra Server to authenticate users. My problem is that ldap_bind() only triggers warnings on PHP. To supress that, I'm using @. But, I would like to be able to identify weather it's a "invalid credentials" or "unable to bind server" problem.

Here is my code:

private function ldap($email, $password) {
    $login = explode('@', $email);
    $name = $login[0];
    $domain = str_replace('.', ',dc=', $login[1]);
    $dn = 'uid=' . $name . ',ou=people,dc=' . $domain;
    $ldap['user'] = $name;
    $ldap['pass'] = $password;
    $ldap['host'] = 'webmail.' . $login[1];
    $ldap['port'] = 389;
    $ldap['dn'] = $dn;
    $ldap['base'] = '';

    $ldap['conn'] = ldap_connect($ldap['host'], $ldap['port']);        
    ldap_set_option($ldap['conn'], LDAP_OPT_PROTOCOL_VERSION, 3);

    // This will cause a notice error level, which cannot be catched. Supress the error and validate afterwards.        
    $ldap['bind'] = @ldap_bind($ldap['conn'], $ldap['dn'], $ldap['pass']);

    if ($ldap['bind']) {
        return true;
    } else {
        throw new NavException('controller.usuario.ldap.password');
    }
}

Upvotes: 0

Views: 6802

Answers (1)

heiglandreas
heiglandreas

Reputation: 3861

You can use ldap_error to get the last error message. See also ldap_errno and ldap_err2str

Upvotes: 2

Related Questions