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

Making Lists - 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
So that takes care of searching. Now, how about adding, editing and deleting entries?

PHP comes with a full-fledged API that allows easy modification of the LDAP directory tree. In order to demonstrate how this API works, I'm going to build, over the next few pages, a simple administration module that performs these functions, so that you can see how it's done.

First up, we need an index page that lists all the entries in the directory. This index page will serve as the starting point for an administrator to make changes to existing directory entries or add new ones. Here's the code,








<html> <head> </head> <body> <table width="450" cellpadding="5" cellspacing="5" border="1"> <?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"); // set base DN and required attribute list $base_dn = "dc=my-domain, dc=com"; $params = array("mail", "cn", "sn"); // list all entries from the base DN $result = ldap_list($conn, $base_dn, "cn=*", $params); ?> <tr> <td>First Name</td> <td>Last Name</td> <td colspan=2>&nbsp;</td> </tr> <?php // get entries $info = ldap_get_entries($conn, $result); // and print attribute values for ($i=0; $i<$info["count"]; $i++) { echo "<tr>"; echo "<td>".$info[$i]["cn"][0]."</td>"; echo "<td>".$info[$i]["sn"][0]."</td>"; echo "<td><a href=\"edit.php?mail=" . urlencode($info[$i]["mail"][0]) . "\">Edit</a></td>"; echo "<td><a href=\"delete.php?mail=" . urlencode($info[$i]["mail"][0]) . "\">Delete</a></td>"; echo "</tr>"; } // all done? clean up ldap_close($conn); ?> </table> <p> <a href="add.html">Add new entry</a> </body> </html>
and here's what it looks like:



As you can see, most of this code is similar to what you saw in the previous article. However, there is one important difference - instead of using the ldap_search() function, I'm using the ldap_list() function, which returns a one-level list of all the entries matching the specified criteria, given a base DN at which to start searching.

<?php // set base DN and required attribute list $base_dn = "dc=my-domain, dc=com"; $params = array("mail", "cn", "sn"); // list all entries from the base DN $result = ldap_list($conn, $base_dn, "cn=*", $params); ?>
This base DN and search filter are provided to ldap_list() as second and third arguments respectively. In the example above, the ldap_list() function returns all the entries which have a "cn" attribute and are located immediately under the node with DN "dc=my-domain,dc=com".

Additionally, ldap_list() accepts a fourth, optional parameter - an array containing a list of all the attributes that should be included in the result set. In the example above, this array is called $params, and it specifies that the returned result set should contain the "cn", "sn" and "mail" attributes.

The search result identifier returned by the ldap_list() can be passed to the ldap_get_entries() function, which does the dirty work of extracting the raw data into a structured array. This array can be processed using a simple "for" loop.

<?php // get entries $info = ldap_get_entries($conn, $result); // and print attribute values for ($i=0; $i<$info["count"]; $i++) { echo "<tr>"; echo "<td>".$info[$i]["cn"][0]."</td>"; echo "<td>".$info[$i]["sn"][0]."</td>"; echo "<td><a href=\"edit.php?mail=" . urlencode($info[$i]["mail"][0]) . "\">Edit</a></td>"; echo "<td><a href=\"delete.php?mail=" . urlencode($info[$i]["mail"][0]) . "\">Delete</a></td>"; echo "</tr>"; } ?>
Note also the links to "edit.php" and "delete.php" next to each entry - I'll be discussing the scripts these links point to shortly. For the moment, though, skip downwards to the last link on the page, which points to "add.html" - this is the HTML form that is used to add new users to the database, and it's discussed on the next page.

 
 
>>> 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: