首页 > 后端开发 > php教程 > LDAP-Series-一-Chapter-3 - PHP-LDAP

LDAP-Series-一-Chapter-3 - PHP-LDAP

WBOY
发布: 2016-06-13 12:33:41
原创
966 人浏览过

LDAP-Series-1-Chapter-3 - PHP-LDAP

If you want to use PHP with ldap. You have to add the extension lib.(ldap.so)

Go to the install file of PHP.

$ cd /opt/php-5.4.5/ext/ldap/
$ /usr/local/bin/phpize
$ ./configure --with-php-config=/usr/local/bin/php-config --with-ldap=/usr/local/openldap
$ make
$ make install
$ cd /usr/local/lib
$ vi php.ini (<strong>extension-dir=/usr/local/lib extension=php_ldap.so</strong>)
$ cp /usr/local/lib/php/ext/..../ldap.so /usr/local/lib/php_ldap.so
登录后复制
You can change the path as you like.

$ php -v
登录后复制
Check if any error exist. (Until now, my work place`s php cannot load ldap.so)

PHP Test file.

<?php class m_ldap{
        private $ldapHost = "127.0.0.1";
        private $ldapPort = 389;
        private $ds;
        function m_ldap_con(){
                $this->ds = ldap_connect($this->ldapHost,$this->ldapPort) or die("Could not connect to $this->ldapHost");
                return true;
        }
        function m_ldap_bind($dn,$psw){
                ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3);
                if($this->ds){
                        $r = ldap_bind($this->ds,$dn,$psw) or die("Could not bind to $dn");
                        if($r){
                                return true;
                        }else{
                                return false;
                        }
                }else{
                        return false;
                }
        }
        function m_ldap_add($dn,$info){
                $r=ldap_add($this->ds,$dn,$info);
                if($r){
                        return true;
                }else{
                        return false;
                }
        }

        function m_ldap_modify($dn,$info){
                $r=ldap_modify($this->ds,$dn,$info);
                if($r){
                        return true;
                }else{
                        return false;
                }
        }

        function m_ldap_search($dn,$filter){
                $ldapSearch = ldap_search($this->ds,$dn,$filter);
                $ldapInfo = ldap_get_entries($this->ds,$ldapSearch);
                return $ldapInfo;
        }
}

?>
登录后复制
ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3); //Make the ds to use PROTOCOL VERSION 3, Otherwise, there will be an error about PROTOCOL.

<?php require_once("m_ldap.php");
        $mdap=new m_ldap();
        $mdap->m_ldap_con();
        $dn = "cn=root,ou=SystemAdmin,dc=xxx,dc=org";
        $psw= "xxxxxx";

        $mdap->m_ldap_bind($dn,$psw) or die("cannot bind");
        
        $sdn="ou=people,ou=iWeb,dc=weiwejia,dc=org";
        $filter="(uid=*)";
        $res=$mdap->m_ldap_search($sdn,$filter);

        foreach( $res[0]["cn"] as $key=>$val){
                print $key."\n";
                print $val."\n";
        }
?>
登录后复制
If everything has been ok, it will works now. (Need ACL support, introduce next chapter.)




相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板