Home arrow PHP arrow Page 2 - Using PHP With LDAP (part 2)

Of Needles And Haystacks - PHP

In this second, and concluding, article, dig deeper into PHP'sLDAP API by writing complex search queries and building a Web-basedadministration module to retrieve and modify entries from the LDAP directorytree.

TABLE OF CONTENTS:
  1. Using PHP With LDAP (part 2)
  2. Of Needles And Haystacks
  3. Making Lists
  4. Adding It All Up
  5. Changing Things Around
  6. Wiping Out The Past
  7. To Err Is Human...
  8. Endgame
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 9
April 07, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
In the first part of this article, I had written a simple PHP script to search the LDAP directory for a particular user. But real life is never that simple...

Let's suppose I want to search for my friend Joe from high school. Now, there are a million Joes in the world, and I would like to drill down to my Joe without having to navigate through a large set of results. The solution? A more complex search, which uses additional parameters to restrict the result set.

Take a look at the new search form,


<html> <head> <title>Search</title> </head> <body> <form action="search.php" method="POST"> First name <br> <input type="text" name="cn" length="30"><br> Last name <br> <input type="text" name="sn" length="30"><br> Email address <br> <input type="text" name="email" length="30"><br> <input type="submit" name="submit" value="Search"> </form> </body> </html>
which looks like this:



Once the form above is submitted, the data entered by the user is sent to the search script "search.php", which actually performs the query - take a look:

<html> <head> </head>

<body>

<?php

// specify the LDAP server to connect to $conn = ldap_connect("localhost") or die("Could not connect to server");

// bind to the LDAP server specified above $r = ldap_bind($conn) or die("Could not bind to server");

// create the search string $query = "(&(cn=" . $_POST['cn'] . ")(sn=" . $_POST['sn'] . ")(mail=" . $_POST['email'] . "))";

// start searching // specify both the start location and the search criteria // in this case, start at the top and return all entries $result = ldap_search($conn,"dc=my-domain,dc=com", $query) or die("Error in search query");

// get entry data as array $info = ldap_get_entries($conn, $result);

// iterate over array and print data for each entry echo "<ul>"; for ($i=0; $i<$info["count"]; $i++) { echo "<li>".$info[$i]["sn"][0]." - ".$info[$i]["mail"][0]." - ".$info[$i]["dn"]."</li>"; } echo "</ul>";

// print number of entries found echo "Number of entries found: " . ldap_count_entries($conn, $result) . "<p>";

// all done? clean up ldap_close($conn);

?>

</body> </html>
The structure of the code is identical to that of the examples in previous pages - with one important difference:

<?php

// create the search string $query = "(&(cn=" . $_POST['cn'] . ")(sn=" . $_POST['sn'] . ")(mail=" . $_POST['email'] . "))";

?>
This search string, obviously with the variables replaced with actual values, is passed to the ldap_search() function; it returns only those entries from the LDAP directory free which match *all* the parameters specified. Why? Because of my usage of the special AND operator, signified by the addition of the ampersand (&) to the beginning of the search string above.

Here's what the output looks like:



If your LDAP entries contain other attributes, it's just as easy to create more complex search queries - simply add more input fields to the search form, and update the search string above to use those attributes when searching the directory tree.

In case you were wondering, yes, you can also use logical OR (the | operator) or logical NOT (the ! operator) in your search queries - I'll leave that to you to play with.

 
 
>>> More PHP Articles          >>> More By Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: