Using PHP With LDAP (part 1) - The Bare Necessities (
Page 3 of 6 )
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.
dn: dc=my-domain, dc=com
objectClass: dcObject
objectClass: organization
o: MyOrganization
dc: my-domain.com
dn: mail=root@my-domain.com, dc=my-domain, dc=com
objectClass: inetOrgPerson
cn: Keith
sn: Richards
mail: root@my-domain.com
dn: mail=joe@my-domain.com, dc=my-domain, dc=com
objectClass: inetOrgPerson
cn: Joe
sn: Somebody
mail: joe@my-domain.com
dn: mail=sarah@my-domain.com, dc=my-domain, dc=com
objectClass: inetOrgPerson
cn: Sarah
sn: Nobody
mail: sarah@my-domain.com
telephoneNumber: 23 67 128 5639
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: