如何识别ldap_bind()上的不同错误? PHP

时间:2014-07-28 14:35:09

标签: php error-handling ldap

我使用ldap_bind连接到Zimbra服务器以验证用户身份。我的问题是ldap_bind()只在PHP上触发警告。为了压制它,我使用@。但是,我希望能够识别天气,这是一个无效的凭证"或"无法绑定服务器"问题

这是我的代码:

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');
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用ldap_error获取最后一条错误消息。另请参见ldap_errno和ldap_err2str