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 (extension-dir=/usr/local/lib extension=php_ldap.so)
$ 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.)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值