2012-01-31 57 views
2

我使用Windows XP與XAMPP 1.6.4 - 啓用LDAP上XAMPP

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

PHP 5.2.4與LDAP,但始終的ldap_bind返回此錯誤: 警告:的ldap_bind()[function.ldap-綁定]:無法綁定到服務器:無法聯繫D:\ xampp \ htdocs \ test.php在第11行的LDAP服務器 布爾(假)

+0

[Here](http://stackoverflow.com/questions/1049653/ldap-and-php)是另一個線程。查看一些答案,因爲答案沒有被接受。 – CoffeeRain 2012-01-31 15:06:51

回答

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; 
    } 
} 
?> 
+0

警告:ldap_bind()[function.ldap-bind]:無法綁定到服務器:無法聯繫第48行的D:\ xampp \ htdocs \ test.php中的LDAP服務器 – 2012-01-31 15:16:51