PHP
  Home arrow PHP arrow Page 5 - 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 - Source Code


    (Page 5 of 6 )

    Here is the complete source code. Simply cut and paste this into a valid HTML document and give it a try.

    <?php

    $LDAP_NAME[0] = "Netscape Net Center";
    $LDAP_SERVER[0] = "memberdir.netscape.com";
    $LDAP_ROOT_DN[0] = "ou=member_directory,o=netcenter.com";

    $LDAP_NAME[1] = "Bigfoot";
    $LDAP_SERVER[1] = "ldap.bigfoot.com";
    $LDAP_ROOT_DN[1] = "";

    //If no server chosen set it to 0
    if(!$SERVER_ID)
    $SERVER_ID=0;

    //Create Query
    $ldap_query = "cn=$common";

    //Connect to LDAP
    $connect_id = ldap_connect($LDAP_SERVER[$SERVER_ID]);

    if($connect_id)
    {
    //Authenticate
    $bind_id = ldap_bind($connect_id);

    //Perform Search
    $search_id = ldap_search($connect_id, $LDAP_ROOT_DN[$SERVER_ID], $ldap_query);

    //Assign Result Set to an Array
    $result_array = ldap_get_entries($connect_id, $search_id);
    }
    else
    {
    //Echo Connection Error
    echo "Could not connect to LDAP server: $LDAP_SERVER[$SERVER_ID]";
    }

    //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 {
    $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 .= "<A HREF=\"ldap://$LDAP_SERVER[$SERVER_ID]/$dn\">$cn</Aglt;";
    $result_list .= " <A HREF=\"mailto:$email\">$email</A><BR>\n";
    }
    }
    }
    else
    {
    echo "Result set empty for query: $ldap_query";
    }

    //Close Connection
    ldap_close($connect_id);

    //Make Form
    echo "<CENTER><FORM ACTION=\"$PHP_SELF\" METHOD=\"GET\">";
    echo "Search in:<SELECT NAME=\"SERVER_ID\">";

    //Loop Through and Create SELECT OPTIONs
    for($i=0; $i<count($LDAP_NAME); $i++)
    echo "<OPTION VALUE=\"$i\">".$LDAP_NAME[$i]."</OPTION>";

    echo "</SELECT><BR>";
    echo "Search for:<INPUT TYPE=\"text\" NAME=\"common\">";
    echo "<INPUT TYPE=\"submit\" NAME=\"lookup\" VALUE=\"go\"><BR>";
    echo "(You can use * for wildcard searches, ex. * Stanley will find all Stanleys)<BR>";
    echo "</FORM></CENTER>";

    //Echo Results
    if($result_list)
    {
    echo "<CENTER><TABLE BORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"10\"
    BGCOLOR=\"#FFFFEA\" WIDTH=\"450\"><TR><TD>$result_list</TD></TR>
    </TABLE></CENTER>";
    }
    else
    echo "No Results";
    }

    ?>

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek