XAMPP localhost上的LDAP

时间:2012-01-31 15:00:52

标签: ldap

我正在使用带有XAMPP 1.6.4的Windows XP - 启用了LDAP的php 5.2.4

trying this script :
$server = "ldap://127.0.0.1/";
$user = "Salman";
$pass = "123";

$con = ldap_connect($server);

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

var_dump(ldap_bind($con, $user, $pass)); 

但ldap_bind始终返回此错误: 警告:ldap_bind()[function.ldap-bind]:无法绑定到服务器:无法联系第11行的D:\ xampp \ htdocs \ test.php中的LDAP服务器 布尔(假)

1 个答案:

答案 0 :(得分:0)

嗨,我把这个课程放在一起了一会儿:

    <?php

    /* USAGE:
     * $ldapObj= new Ldap("HOST","PORT","USERNAME","PASSWORD","Distinguished Name","        (mailnickname=USERNAME)",array("*"));
    */

    class Ldap {

    enter code here
    public $ldaphost;
    public $ldapport;
    public $ldaprdn;
    public $ldappass;
    public $dn;
    public $filter;
    public $attributes;
    public $ad;
    public $ldapbind;
    public $ldapresults;
    public $ldapentries;
    public $ldaperror;
    public $ldapstatus;

    function __construct($ldaphost, $ldapport, $ldaprdn, $ldappass, $dn, $filter, $attributes) {
        $this->ldaphost = $ldaphost;
        $this->ldapport = $ldapport;
        $this->ldaprdn = $ldaprdn;
        $this->ldappass = $ldappass;
        $this->dn = $dn;
        $this->filter = $filter;
        $this->attributes = $attributes;
        $this->ad = ldap_connect($this->ldaphost);
        if ($this->ad) {

            $this->ldapbind = ldap_bind($this->ad, $this->ldaprdn, $this->ldappass);
            if ($this->ldapbind) {
                $this->ldapresults = ldap_search($this->ad, $this->dn, $this->filter, $this->attributes);
                $this->ldapentries = ldap_get_entries($this->ad, $this->ldapresults);
                $this->getSid();
                $this->ldapstatus = 1;
            } else {
                $this->ldaperror = "Unable to authenticate user";
                $this->ldapstatus = 0;
            }
            ldap_unbind($this->ad);
        } else {
            $this->ldaperror = "Could not connect to {$this->ldaphost}";
            $this->ldapstatus = 0;
        }
    }

    function getLdapEntry($entry) {
        return $this->ldapentries[0][$entry][0];
    }

    function getLdapEntries() {
        return $this->ldapentries;
    }

    function ldapError() {
        return $this->ldaperror;
    }

    function getSid() {
        $sid = "S-";
        $entries = $this->ldapentries;
// Convert Bin to Hex and split into byte chunks
        $sidinhex = str_split(bin2hex($entries[0]['objectsid'][0]), 2);
// Byte 0 = Revision Level
        $sid = $sid . hexdec($sidinhex[0]) . "-";
// Byte 1-7 = 48 Bit Authority
        $sid = $sid . hexdec($sidinhex[6] . $sidinhex[5] . $sidinhex[4] . $sidinhex[3] . $sidinhex[2] . $sidinhex[1]);
// Byte 8 count of sub authorities - Get number of sub-authorities
        $subauths = hexdec($sidinhex[7]);
//Loop through Sub Authorities
        for ($i = 0; $i < $subauths; $i++) {
            $start = 8 + (4 * $i);
            // X amount of 32Bit (4 Byte) Sub Authorities
            $sid = $sid . "-" . hexdec($sidinhex[$start + 3] . $sidinhex[$start + 2] . $sidinhex[$start + 1] . $sidinhex[$start]);
        }
        $this->sid = $sid;
    }
}
?>