Among its many other capabilities, PHP also comes with afull-featured API to connect to, and communicate with, LDAP directoryservers. This article explores how PHP and LDAP can be used together,beginning with a crash course in LDAP basics and proceeding to a series ofsimple examples that demonstrate how PHP can be used to search an LDAPdirectory and format the results for the Web.
In order to get started with PHP and LDAP, there are a couple of things you need.
First, you need a LDAP server, which you can use as the foundation for your PHP scripts. While there are many commercial LDAP servers, the one used in this article is the OpenLDAP server, which is robust, scalable and - best of all - free. You can download a copy from http://www.openldap.org(this tutorial uses OpenLDAP 2.1.16).
Once you've got your LDAP server up and running, it's time to populate it with some entries. Here are the sample entries that I will be using throughout this tutorial.
Refer to the OpenLDAP reference manual, or the usage guide
that shipped with your LDAP server, for the exact commands needed to import this data into your LDAP directory. Note that the server must be set up to allow read access without a password, and its base DN should be "dc=my-domain, dc=com", as in the sample data above.
Once the LDAP server has been set up, the next step is to configure PHP to communicate with this server. PHP 4.3 supports this via its LDAP extension, which is not activated by default - you may need to recompile your PHP binary with the "--with-ldap" parameter to activate LDAP support (Windows users get a pre-built binary with their distribution).
Once you've got PHP configured with LDAP support, check to see that all is well via a quick call to phpinfo() - you should see something like this: