php ldap

php支持ldap接入,但是需要安装php-ldap扩展。安装很简单这里不再赘述。

以下是我在yii框架内实现的ldap测试代码,除了日志记录,其它的都是原生代码。

示例代码

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

在调试的时候遇到了不少的问题,下面是问题记录:

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参数有问题
发布了105 篇原创文章 · 获赞 58 · 访问量 41万+

猜你喜欢

转载自blog.csdn.net/ljl890705/article/details/105032568