Home arrow PHP arrow Page 6 - Using PHP With LDAP (part 1)

What's In A Name? - PHP

Among its many other capabilities, PHP also comes with afull-featured API to connect to, and communicate with, LDAP directoryservers. This article explores how PHP and LDAP can be used together,beginning with a crash course in LDAP basics and proceeding to a series ofsimple examples that demonstrate how PHP can be used to search an LDAPdirectory and format the results for the Web.

TABLE OF CONTENTS:
  1. Using PHP With LDAP (part 1)
  2. Looking For Answers
  3. The Bare Necessities
  4. Code Poet
  5. Anatomy 101
  6. What's In A Name?
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 34
April 04, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
The example on the previous page was fairly static - all it did was return a list of all the entries in the directory that had a "cn" attribute. This next example makes things a little more interactive - it includes a form where the user can enter a name and search the LDAP server for that name.

Here's the form,

<html> <head> <title>Search</title> </head> <body> <form action="search.php" method="POST"> <input type="text" name="name" length="30"> <input type="submit" name="submit" value = "Search"> </form> </body> </html>
and here's what it looks like:



And here's the code for the search script:

<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['name'] . ")"; // 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]["cn"][0] ." - ".$info[$i]["mail"][0] . "</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 only difference between this script and the previous one is that this time, the search criteria is dynamically generated using the data POSTed from the form.

$query = "(cn=" . $_POST['name'] . ")";
This parameter is then passed to the ldap_search() function and processed in the standard manner.

Here's what you should see, assuming that you searched for the string "joe":



Simple when you know how, isn't it?

And that's about it for this first part. In the second part of this article, I will be showing you how to carry out more complex searches, and also add and delete information from the LDAP directory. Stay tuned for that one.and, until next time, stay healthy!

Note: Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!

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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: