Home arrow PHP arrow Page 3 - Create an LDAP Address Book with PHP
PHP

Create an LDAP Address Book with PHP


What is LDAP and how does it relate to PHP? If you're unsure, then Dannie's come to save the day! In this article he explains what LDAP is and shows you how to create an LDAP address book with PHP...

Author Info:
By: Dannie Stanley
Rating: 4 stars4 stars4 stars4 stars4 stars / 46
January 08, 2003
TABLE OF CONTENTS:
  1. · Create an LDAP Address Book with PHP
  2. · Setup Public LDAP Server Information
  3. · Format Output
  4. · Close Connection
  5. · Source Code
  6. · Conclusion

print this article
SEARCH DEVARTICLES

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".
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- 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

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials