Page 1 of 1

How to get LDAP attributes with PHP

Posted: Sat Nov 28, 2009 8:06 am
by nithinkk
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


im unable to get the LDAP attibutes. But i can able to connect the LDAP server My code is

Code: Select all

 
 <? 
 
 
 
require("ldapclass.php"); 
 
ini_set('memory_limit','64M');
 
$ldap = new ldap("dc 192.168.05","389","dc=sholen,dc=edu");  
 
$ldap->ldapConn(); 
 
    //binding with the database using username and password
 
    
    $bind=$ldap->ldapBind("XXXX@sholen.edu","YYYYY"); 
 
    //fixing directory name
 
    $ouArray = array("staff"); 
 
    //searching the department name  for all the users in staff
 
    $results = $ldap->ldapSearch("department","*",$ouArray,"department"); 
 
 
    $i=0; 
 
    $results= array_unique($results);
 
    
 
    //rearranging array
 
    foreach($results as $output)
    
 
    { 
 
 
        $name[$i]=$output;
        
 
        echo " $name[$i] <br>";
        $i++;
 
    }
 
 
 
    
 
    //ldap authentication
 
    $auth = $ldap->ldapAuthenticate("search@sholen.edu","sholen123"); 
 
    
 
 
 
        
 
                ?>


The above code list the Departments in my ldap server....i want to list the attributes in my LDAP server...can any one HELP me please ????


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: How to get LDAP attributes with PHP

Posted: Mon Nov 30, 2009 10:43 am
by pickle
You're obviously using an LDAP class to talk to LDAP, but you haven't posted the code for that class - so there's not a lot we can do.

When talking to LDAP, you set the attributes you want to retrieve when you do the query. Check the documentation for your LDAP class to see if there's a setAttributes() or some method to name the attributes you want.

Re: How to get LDAP attributes with PHP

Posted: Mon Nov 30, 2009 5:51 pm
by Weiry
Are you sure your able to connect with that code?
Because i picked up on this:

Code: Select all

"dc 192.168.05"
Which i am fairly sure does not make up an IP address. Unless of course you made a typo and forgot to separate it to "dc 192.168.0.5".

Re: How to get LDAP attributes with PHP

Posted: Tue Dec 08, 2009 6:14 am
by nithinkk
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


My LDAP Class is

Code: Select all

 
<? class ldap
 
{
 
  var $ldapConn; //ldap connection storage variable
 
  var $ldapBind; //ldap bind storage variable
 
  var $entries;  //ldap entries variable
 
  var $ldapLookupUser;
 
  var $ldapLookupPass;
 
  var $server;
 
  var $port;
 
  var $by;
 
  var $search;
 
  var $baseDN;
 
 
 
 
 
  function ldap($server,$port,$baseDN)
 
  {
 
    $this->server=$server;
 
    $this->port=$port;
 
    $this->baseDN=$baseDN;
   
 
  }
 
 
 
 
 
 
  function ldapConn()
 
  {
       
 
    $this->ldapConn = @ldap_connect($this->server,$this->port);
     return $this->ldapConn;
 
  }
 
 
 
 
 
  function ldapBind($ldapLookupUser,$ldapLookupPass)
 
  {
 
    if(@ldap_bind($this->ldapConn,$ldapLookupUser,$ldapLookupPass))
 
    {
 
      $this->ldapBind = @ldap_bind($this->ldapconn,$ldapLookupUser,$ldapLookupPass);
 
      return true;
 
    }
 
    else
 
      return false;
 
  }
 
 
 
 
 
 
 
  function ldapAuthenticate($usrname,$password)
 
  {
 
    if(@ldap_bind($this->ldapConn,$usrname,$password))
 
      return true;
 
    else
 
      return false;
 
  }
 
 
 
 
 
  function ldapSearch($by,$search,$ous,$searchby)
 
  {
 
    $c=0;
 
    foreach($ous as $ou)
 
    {
 
      $read=ldap_search($this->ldapConn,"ou=$ou,$this->baseDN", "$searchby=$search");
 
      $entries = ldap_get_entries($this->ldapConn, $read);
 
      for ($i=0; $i<$entries["count"]; $i++)
 
      {
 
        if($entries[$i][$by][0])
 
          $values[$c]=$entries[$i][$by][0];
 
        $c++;
 
      }
 
    }
 
    return $values;
 
  }
 
  
 
}
 
 
 
?>

pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: How to get LDAP attributes with PHP

Posted: Mon Dec 14, 2009 9:46 am
by pickle
I've already corrected 2 of your code posts in this thread please take an extra 2 seconds & make sure your code is posted properly.

Re: How to get LDAP attributes with PHP

Posted: Tue Dec 15, 2009 12:35 am
by nithinkk
The above code list the Departments in my ldap server....i want to list the attributes in my LDAP server...can you HELP me adding the code for getting attributes in same example ????

Re: How to get LDAP attributes with PHP

Posted: Mon Dec 21, 2009 10:01 am
by pickle
Check the docs for ldap_search() - it explains how to ask for certain attributes.