Home arrow PHP arrow Page 5 - 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 - 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";
}

?>

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