Jump to content

Searching LDAP


Pioden

Recommended Posts

LDAP questions don't seem to get many responses here but undeterred I'm going to give it a go!

 

I'm writing a little Ajax/PHP combo to search our college (LDAP) address book. I'm slowly getting to grips with the AJAX bits but the LDAP searching is giving me a serious headache!

 

My code at the moment returns the entire LDAP record for the users it finds. Reams of stuff!!  But all I really need is  "departmentnumber" and "maillocaladdress" for matches to "uid".

 

The equivalent SQL query would be simple:

SELECT departmentnumber,maillocaladdress,uid FROM ldap WHERE uid = searchname

(something like that anyway!)

 

Can anybody point me in the right direction with filtering the LDAP search results? The stuff I've found on-line is confusing to say the least.

 

TIA

 

Huw

 

<?php





$username = "huw.*";



// LDAP variables

$ldaphost = "ldapslave.mycollege.ac.uk";  // your ldap servers

$ldapport = 389;                 // your ldap server's port number



// Connecting to LDAP

$ds = ldap_connect($ldaphost, $ldapport) or die ("Could not connect to $ldaphost");



//Connection made -- bind anonymously and get dn for username.

    $bind = @ldap_bind($ds);

    

//Check to make sure we're bound.

    if( !bind )

    {

        echo "Anonymous bind to LDAP FAILED.";

        exit;

    }


// This is the stuff I can't get to work!	

//	$filter="(|(uid=$username*))";

//	$justthese = array("uid", "departmentnumber", "maillocaladdress"); 



$search =  ldap_search($ds, "dc=mycollege,dc=ac,dc=uk", "uid=$username"/*$filter, $justthese*/);

      

$info = ldap_get_entries($ds, $search);





print_r ($search);




?>

 

 

Link to comment
Share on other sites

OK. I'm still hoping for replies - but I've also made a little progress!

 

I've managed to filter out most of the info I don't need by modifying the query with some filtering

 

$justthese = array("uid", "departmentnumber", "maillocaladdress"); 

$search =  ldap_search($ds, "dc=mycollege,dc=ac,dc=uk", "uid=$username", $justthese);

$info = ldap_get_entries($ds, $search);

print_r ($info);

 

This gives me all the data I need - but with a whole bunch of array info. Here's a snippet.

 

Array ( [count] => 2 [0] => Array ( [uid] => Array ( [count] => 1 [0] => username1 ) [0] => uid [maillocaladdress] => Array ( [count] => 1 [0] => useremail ) [1] => maillocaladdress [departmentnumber] => Array ( [count] => 1 [0] => Engineering ) [2] => departmentnumber [count] => 3 [dn] => uid=username,ou=Users,dc=mycollege,dc=ac,dc=uk ) 

 

So the follow on question is how do I turn this into something useful? Is this a job for 'foreach' ? Sorry my brain is pretty fried at the moment!!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.