How to get LDAP attributes with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

How to get LDAP attributes with PHP

Post 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.
Last edited by pickle on Mon Nov 30, 2009 10:41 am, edited 1 time in total.
Reason: Removed ldap username & password
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to get LDAP attributes with PHP

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: How to get LDAP attributes with PHP

Post 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".
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Re: How to get LDAP attributes with PHP

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to get LDAP attributes with PHP

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
nithinkk
Forum Commoner
Posts: 55
Joined: Sat Nov 28, 2009 7:57 am

Re: How to get LDAP attributes with PHP

Post 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 ????
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to get LDAP attributes with PHP

Post by pickle »

Check the docs for ldap_search() - it explains how to ask for certain attributes.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply