Build a directory service for web-based services

OpenLDAP for the people

Run addEntry.html in a browser. Specify a directory entry's values and click on Add Entry.

Adding a directory entry

Adding a directory entry

The directory entries added to the OpenLDAP server get listed in the LDAP Browser.

New directory entry

New directory entry

Modify a directory entry

Create a PHP script, modify_entry.php, to modify a directory entry. Create a connection resource and bind with the LDAP directory. Create a directory entry variable consisting of an array of attributes with the modified values. For example, modify the "title" attribute value and the "telephoneNumber" attribute value. The attribute values are obtained from an input HTML form, modifyEntry.html

$directory_entry["title"]=$_GET['title'];
$directory_entry["telephoneNumber"]=$_GET[' telephoneNumber'];

A directory entry is identified with a distinguished name. Specify the dn of the directory entry to modify.

$dn="cn=".$_GET['cn'].",dc=example,dc=com";

Modify the directory entry with ldap_modify().

$r=ldap_modify($ldapconn,$dn, $directory_entry);

Run the input form to modify a directory entry in a browser. Specify the modified values and click on Modify Entry. The directory entry values should now be modified.

Search a directory entry

In this section we will use a PHTML (PHP embedded in HTML) script to search for a directory entry and display the result. Add the .phtml extension to the AddType configuration directive in httpd.conf file and restart Apache web server.

AddType application/x-httpd-php .php .phtml

Create a PHTML script, search_entry.phtml and create a connection resource and bind with the directory server. Next, specify an attribute array, which specifies attributes to be retrieved. By default, all the attributes are retrieved.

$attribute_array=array("cn", "sn", "title", "telephoneNumber","postalCode","postalAddress");

Specify the dn of the directory entry to search. The cn attribute value is specified in an input form searchEntry.html.

$dn="cn=".$_GET['cn'].",dc=example,dc=com";

Specify a filter for the search. For example, specify a filter that searches for all object classes.

$filter = "(objectclass=*)";

Search the directory using ldap_search().

$sr=ldap_search($ldapconn,$dn, $filter, $attribute_array);

Retrieve the directory entries in the search result using ldap_get_entries().

$directory_entries= ldap_get_entries($ldapconn, $sr);

Create an HTML table to display the search result. Run the searchEntry.html page in a browser. Specify the first name of a member. The member information for the specified member name should now be displayed.

Search result

Search result

A directory entry may be deleted using ldap_delete().

That's it. You now know how create a directory service for users of an online service, such a social network, using PHP with OpenLDAP.®

Similar topics


Other stories you might like

Biting the hand that feeds IT © 1998–2022