This article demonstrates how to use PHP to connect to an LDAP server. The specific example is to connect to a public LDAP server and perform a search. This example imitates NetscapeCommunicator4. * and connects to LDAP resources through its own address book. LDAP prefix
This article demonstrates how to use PHP to connect to an LDAP server. The specific example is to connect to a public LDAP server and perform a search. This example imitates Netscape Communicator 4. * and connects to LDAP resources through its own address book.
LDAP prefix
Many people may have heard about LDAP, but they do not know what it is and how it works. Here, I will not introduce LDAP very specifically, but just give an introduction to this protocol.
LDAP is a protocol used to declare directory information to many different resources. It is usually used as a centralized address book application, but according to the organizer's needs, it can be more powerful.
The most basic form of LDAP is a scale method for connecting to the database. The database is optimized for read queries. Therefore, it can quickly obtain query results, but it is much slower in other aspects, such as updates. Note that LDAP is usually used as a hierarchal database application, rather than a relational database. Therefore, the structure of the table is better than that of the table. Because of this, you cannot use SQL statements.
In short, LDAP is a quick way to obtain centralized and static data about people or resources.
Request
PHPV.4 (the previous version is acceptable, but it has not been tested). The compilation supports LADP, that is, the ldap directory with the -- with-LDAP public during application compilation. In this example, two instances are provided.
Example overview
1. set information about the public LDAP server
2. create an LDAP query
3. connect to the LDAP server
4. if the connection is successful, process the query
5. pattern output
6. closed connection
7. design the HTML table on the search interface
8. display results
Set public LDAP server information
The first thing we need to do is to define all the information of the LDAP server to be searched.
'Ldap _ name' = NAME of the new LDAP object
'Ldap _ server' = target IP address or host name of the new LDAP entry
'Ldap _ ROOT_DN '= identification name of the new LDAP project root
<? 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] = '';
// Set it to 0 if no server is selected
If (! $ SERVER_ID)
$ SERVER_ID = 0;
?>
Create an LDAP query
As mentioned above, LDAP queries are different from SQL queries. Therefore, the statement must be limited. The following is a basic example.
// Create Query $ ldap_query = 'CN = $ common ';
In our example, "cn" is the attribute to be searched, and $ common is the string variable obtained from the form to be searched. The wildcard '*' can be applied to the LDAP query statement '*'. For example, '$ stanley' can find 'dan stanley '.
Connect to the LDAP server
The following functions connect to an LDAP resource and assign the connection identification name to a variable, just like connecting to a common database, such as MySQL.