PHP
  Home arrow PHP arrow Page 3 - Create an LDAP Address Book with PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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? 
PHP

Create an LDAP Address Book with PHP
By: Dannie Stanley
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 32
    2003-01-08

    Table of Contents:
  • Create an LDAP Address Book with PHP
  • Setup Public LDAP Server Information
  • Format Output
  • Close Connection
  • Source Code
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Create an LDAP Address Book with PHP - Format Output


    (Page 3 of 6 )

    When an LDAP search is performed, the data is returned in whatever sequence it is found. In other words, there is not an easy way to sort the data like with a common SQL ORDER BY statement. As well, many public LDAP directories do not have standard capitalization.

    Since the sort is based on the ASCII value of the strings, we must format the strings with all lowercase letters to appropriately alphabetize our output.

    It is important to note, that an LDAP result set is returned as a multi-dimensional array. Thus, at this point in our script $result_array contains something like this:

    $result_array[0]["cn"] [0] = "Dannie Stanley"
    ["dn"] [0] = "uid=dannie,dc=spinweb.net"
    ["givenname"][0] = "Dannie"
    ["sn"] [0] = "Stanley"
    ["mail"] [0] = "danSPAM@spinweb.net"
    $result_array[1]["cn"] [0] = "Michael Reynolds"
    ["dn"] [0] = "uid=michael,dc=spinweb.net"
    ["givenname"][0] = "Michael"
    ["sn"] [0] = "Reynolds"
    ["mail"] [0] = "michaelSPAM@spinweb.net"


    The data is stored in this format because each attribute may have more than one value (IE a tree structure).

    For example, if my name is 'Dannie,' yet everyone knows me as 'Dan,' I could add an attribute to LDAP to store both representations of my given name like this:

    $result_array[0]["cn"] [0] = "Dannie Stanley"
    ["dn"] [0] = "uid=dannie,dc=spinweb.net"
    ["givenname"][0] = "Dannie"
    ["givenname"][0] = "Dan"
    ["sn"] [0] = "Stanley"
    ["mail"] [0] = "danSPAM@spinweb.net"


    For this search, we are only worried about the first value of every attribute so we will be using 0 as the index for each attribute, except for dn (Distinguished Name), which contains only one value.

    Here is a brief list of attributes and their meaning:
    "cn" = Common Name
    "dn" = Distinguished Name
    "givenname" = First Name
    "sn" = Last Name
    "mail" = Email Address

    <?php

    //Sort results if search was successful
    if($result_array)
    {
    for($i=0; $i {
    $format_array[$i][0] = strtolower($result_array[$i]["cn"][0]);
    $format_array[$i][1] = $result_array[$i]["dn"];
    $format_array[$i][2] = strtolower($result_array[$i]["givenname"][0]);
    $format_array[$i][3] = strtolower($result_array[$i]["sn"][0]);
    $format_array[$i][4] = strtolower($result_array[$i]["mail"][0]);
    }

    //Sort array
    sort($format_array, "SORT_STRING");

    for($i=0; $i<count($format_array); $i++)
    {
    $cn = $format_array[$i][0];
    $dn = $format_array[$i][1];
    $fname = ucwords($format_array[$i][2]);
    $lname = ucwords($format_array[$i][3]);
    $email = $format_array[$i][4];

    if($dn && $fname && $lname && $email)
    {
    $result_list .= "<A HREF=\"ldap://$LDAP_SERVER[$SERVER_ID]/$dn\">$fname $lname</A>";
    $result_list .= " <A HREF=\"mailto:$email\">$email</A><BR>\n";
    }
    elseif($dn && $cn && $email)
    {
    $result_list .= "$cn";
    $result_list .= " <$email>
    \n";
    }
    }
    }
    else
    {
    echo "Result set empty for query: $ldap_query";
    }

    ?>

    In our example, $format_array is our new array which contains the query results in a format optimized for output.

    First, we loop through every element of the $result_array and assign it to a two-dimensional array for sorting purposes. At the same time we are using the strtolower() function to make all values lower-case.

    Second, we sort the array using a handy little search algorithm provided by PHP called sort(). The first argument is the array. The second is what type of sorting to perform, as defined by the PHP documentation. Since we are sorting by string, we use "SORT_STRING".

    Third, we loop through the newly formatted array and assign it to an output string named $result_list that contains the HTML representation of the data. It is important to note that I have used the ldap URL format for the hyper-links. An example of this looks something like this:

    HREF="ldap://ldap.domain.net/uid=dannie,dc=domain.net".

    More PHP Articles
    More By Dannie Stanley


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







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