Home arrow PHP arrow Page 2 - 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 - Setup Public LDAP Server Information
(Page 2 of 6 )

The first thing we need to do is define all of the LDAP servers we might want to search.

"LDAP_NAME" = The name of the new LDAP entry.
"LDAP_SERVER" = The IP address or hostname of the new LDAP entry.
"LDAP_ROOT_DN" = The root distinguished name of the new LDAP entry.

<?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 LDAP Query

As mentioned previously, LDAP queries are not much like SQL queries. Therefore, the syntax may seem a bit limiting, but here is a basic example and one that works in this scenario.

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

In our example "cn" is the attribute on which we are performing the search, and $common is the search string variable from the search form.

LDAP query syntax allows for wildcard matching using '*'. For example, '*stanley' will find 'dan stanley'.

Connect to LDAP Server

The given function connects to an LDAP resource and assigns the connection link identifier to a variable, much like connecting to a regular database, like MySQL.

<?php

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

?>


In our example, "$connect_id" is the link identifier, $LDAP_SERVER is the array of possible ldap servers, and $SERVER_ID is the LDAP server variable from the search form.

Process Query if Connection Was Successful

If our connection was successful, we will have a valid LDAP link identifier and we can process the query.

<?php

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]";
}

?>


Once we have established a connection to the LDAP services, we must identify ourselves. Most database connections with PHP send the username and password with the connection.

However, with LDAP, credentials are unknown until a bind is performed. In our example, "$bind_id" is the bind link identifier.

We are performing an anonymous bind to the public LDAP servers. Therefore, no argument is sent to ldap_bind() accept the connection link identifier.

After we have been authorized, via bind as anonymous, we perform the query using the ldap_search() function. $search_id is created and is our search link identifier.

Then, we assign our result set to the variable $result_array using the function ldap_get_entries(). This will allow us to sort the information in a logical manner for display.
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