PHP
  Home arrow PHP arrow Page 2 - Using PHP With LDAP (part 2)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Using PHP With LDAP (part 2)
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2003-04-07


    Table of Contents:
  • Using PHP With LDAP (part 2)
  • Of Needles And Haystacks
  • Making Lists
  • Adding It All Up
  • Changing Things Around
  • Wiping Out The Past
  • To Err Is Human...
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using PHP With LDAP (part 2) - Of Needles And Haystacks
    ( Page 2 of 8 )

    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
     

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek