简体   繁体   中英

PHP ldap_connect + ldap_bind unable to bind

I have this PHP code:

$ldap = ldap_connect("aaa.bbbbb.cc")
    or die("Could not connect to LDAP server.");

ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);

$ldapbind = ldap_bind($ldap);

if ($ldapbind) {
    echo "LDAP bind successful...";
} else {
    echo "LDAP bind failed..."; //end up here
}

And I'm unable to make it bind successfully, I have tried all sorts of combinations. Passing credentials, using an URI with "ldap://".

The following C# code works fine when binding to the same LDAP server:

var connection = new LdapConnection("aaa.bbbbb.cc");
connection.Bind();

The LDAP API for PHP seemssomewhat hard to debug, since if bind fails, you don't get any result, so I have no idea how to see what fails, if it cant access the server, bad credentials, or something else..

So, any ideas what could cause the PHP code to fail? is there something special I need to do? (LDAP extension is enabled for PHP)

I've had problems with PHP LDAP before.

Luckily I found this in the doc comments:

ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);

Which will give you verbose output when trying to open a connection and bind.

I usually do this:

 ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
 $ldap = ldap_connect("ldap://" . $domainController, $port);
 $bind = @ldap_bind($ldap, $username . $accountSuffix, $password);
 if (!$bind) { 
     // throw or something
     echo ldap_error($ldap); // http://php.net/manual/en/function.ldap-error.php
 }

Wikipedia has a nice write up of the various parts to the LDAP protocol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM