phpldapldapconnection

ldap_connect() function in php accepts any value and doesnt throw an error


I am writing a php code to connect to my LDAP server.

$adServer = $ini['ldap'];
$ldap = ldap_connect($adServer) or die("Could not connect to {$adServer}");

The Value for $adServer I am fetching from a configuration file.

Looks like ldap_connect() is not throwing an error when I pass blank value or any other random value like "Hello".

I tried giving the below code to check if any error message was generated.

echo ldap_error($ldap)

It always says 'Success'.

Hence I am not able to authenticate if the connection was established or not to the LDAP Server and throw an appropriate error message.

In what situation does the 'die' get triggered for ldap_connect() function. I would like to throw an appropriate error message to the end user if the Server Name provided in the configuration file is not working.

Note: I am using Version 5.6 for PHP


Solution

  • I found a better way to do authenticate instead of using die. After ldap connect, we would continue using ldap bind. If the bind fails, then we can check for the ldap error.

    $ldap = @ldap_connect($adServer);
    $bind = @ldap_bind ($ldap, $ldaprdn, $password);
    if (!$bind) {  // If Bind Failed then.
        if (ldap_errno ($ldap) == 49 {
            //Invalid Credentials
        } else {
        //LDAP Connection to LDAP Server Failed
        }
    }
    

    For a list of all the LDAP Error Number, you can check here