php ldap access support, but needs to be installed php-ldap extension. Installation is very simple and will not repeat them here.
The following is the code I realized ldap test in yii framework, in addition to logging, the other is native code.
Sample Code
public function actionTestLdap()
{
$serverAddr = '10.1.1.1';
$pswd = '123456';
$dn = 'cn=admin01,ou=peo,dc=110,dc=com';
try{
$adConn = ldap_connect($serverAddr);
ldap_set_option($adConn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($adConn, LDAP_OPT_REFERRALS,0);
$adBind = ldap_bind($adConn, $dn, $pswd);
//return $adBind; //若只是校验账户信息是否正确,在此处返回也可以达到校验的目的
$filter = "(cn=*)";
$adSearch = ldap_search($adConn, $dn, $filter);
$infos = ldap_get_entries($adConn, $adSearch);
print_r($infos);
} catch (\Exception $e) {
$msg = "error code:". $e->getCode() .",error message:". $e->getMessage();
Yii::warning($msg);
return false;
}
}
When debugging encountered a lot of problems, here is the problem record:
Error: ldap_bind(): Unable to bind to server: Invalid DN syntax
ldap_bind参数有问题
Error: ldap_bind(): Unable to bind to server: Protocol error
添加配置项ldap_set_option($adConn, LDAP_OPT_PROTOCOL_VERSION, 3);
Error: ldap_bind(): Unable to bind to server: Invalid credentials
ldap_bind参数有问题
Error: ldap_search(): Search: No such object
ldap_search参数有问题