Forum Moderators: open

Message Too Old, No Replies

AJAX and LDAP/Active Directory

Ajaz Ldap ad

         

urbanmac

5:14 pm on Jan 30, 2007 (gmt 0)

10+ Year Member



Hi,

I'm looking at ways of getting a php search of LDAP/AD to return results with out a page refresh. So as the user types the search results begin to appear.

Has anyone come across such a script?

Thanks

StupidScript

5:55 pm on Jan 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's the basic idea: Every "onkeydown" triggers a Javascript/AJAX request and dumps the output into a div (or iframe, if you want to go old school). The request uses a regexp (i.e. MySQL: "..where column regexp '^user_input.*$'...) to compare what's already in the typing field with what's in the db. (The regexp says 'where what the user is typing begins the column entry')

So :

typed: t
match: taller, tasty, teletubby, tuba
typed: ta
match: taller, tasty
typed: tas
match: tasty
, etc.

If you can locate a basic AJAX script that sends a query to a db, you should be able to modify it pretty easily.

[edited by: StupidScript at 5:55 pm (utc) on Jan. 30, 2007]

urbanmac

9:53 pm on Jan 31, 2007 (gmt 0)

10+ Year Member



Hi, thanks for the reply

But I understand how an ajax search works but getting it to work with Active Directory is the problem.

StupidScript

12:05 am on Feb 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay. You'll want to review this section [us2.php.net] of the PHP manual for info on connecting to and getting data from an LDAP tree. You're basically looking at querying a database (connect/bind/query/close). (Note that PHP needs to be compiled
--with-ldap
.)

urbanmac

4:11 pm on Feb 5, 2007 (gmt 0)

10+ Year Member



yes I understand that.

I have another thread on this subject

[webmasterworld.com...]

StupidScript

6:01 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You understand the client side and you understand the server side ... so what is the problem, exactly? Are you having trouble setting up the db connection or something?

StupidScript

8:47 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay ... you're multiple posting ... I'm not going to reply to this thread, any more.

urbanmac

8:49 pm on Feb 5, 2007 (gmt 0)

10+ Year Member



what I cant seem to do is pass the ldap/ad search on to an ajax search.

my ajax script is based on the url being something like search?q=bob but the ldap/ad does not seem to construct the search the same way....so now I'm stumped.

whoisgregg

2:14 pm on Feb 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change your server side script to use $_GET rather than $_POST and you'll be all set.

urbanmac

3:13 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



yes thats exactly what I did - it works perfectly!

urbanmac

3:25 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



I now just need to search two OU points in the same directory at the same time, but adding in a second dn ($dn2) does not work. What's the best way to concatenate the two search DN's?

Heres part of the code:

// Create the DN
$dn = "OU=user,OU=accounts,OU=UK,OU=regions,DC=DOMAIN,DC=com";
$dn2 = "OU=user,OU=accounts,OU=US,DC=DOMAIN,DC=com";

// Specify only those parameters we're interested in displaying
$attrs = array("displayname","mail","samaccountname","telephonenumber","givenname");

// Create the filter from the search parameters
$filter = "(¦(givenName=".$_GET['q']."*) (sn=".$_GET['q']."*) (samaccountname=".$_GET['q']."*) (telephonenumber=".$_GET['q']."*))";

$search = ldap_search($ad, $dn, $dn2, $filter, $attrs)
or die ("<span class='LSstyle_noresults'><strong>Sorry - could not connect to AD</strong></span>");

$entries = ldap_get_entries($ad, $search);

if ($entries["count"] > 0) {