tags:

views:

159

answers:

2

I am trying to connect to ldap with a php web application. If the username/password is correct everything works fine however if the username/password is incorrect I get the following error:

PHP Warning: ldap_bind() [function.ldap-bind]: Unable to bind to server: Invalid credentials

for some reason I am not able to look at the errorno or the state of the bind variable afterwards to print the appopriate user friendly error message.

any ideas?

+1  A: 

The problem is most likely that ldap_errno takes the ldap connection resource, and as ldap_bind failed you can't use it's result to retrieve the errno.

Also: have you tried suppressing the error via the @ sign?

e.g.:

$ldapconn = ldap_connect("localhost");
@$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);

if( $ldapbind )
{
  // Everything went fine
} else {
  // Use the connection resource for ldap_errno
  $errno = ldap_errno( $ldapconn );

  // Check the error number, print an error message (...)
}
lamas
thanks, I didn't know about the surpress option using the @ sign. It solved the problem ;)
MMAMail.com
+1  A: 

You have to check if your ldap connection is ok before trying to bind :

$ldap_conn = ldap_connect($ldap_host,$ldap_port);
if ($ldap_conn) {
    ldap_bind($ldap_conn, $ldap_user_dn, $ldap_pass);
    ...
}
Benjamin Delichère
His problem is regarding the error message which shows up on failure of `ldap_bind`. How could checking the connection resource help?
lamas
You're right.Neverthless ldap_bind expects a valid resource link identifier as first parameter so it's worth testing it before trying to bind.I'm not a big fan of the @ sign, it sometimes hide the real problem.
Benjamin Delichère
He wants to hide the PHP error from the user to output a user-friendly error himself. Where is the problem with the @ sign here?
lamas