Create LDAP Query
syntax may seem a bit limiting, but here is a basic example and one that works in
//Create Query $ldap_query = "cn=$common";
and $common is the search string variable from the search form. LDAP query
syntax allows for wildcard matching using ‘*’. For example, ‘*stanley’ will find
Connect to LDAP Server
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]); ?>
array of possible ldap servers,
and $SERVER_ID is the LDAP server variable from the search form.
Process Query if Connection Was Successful
we can process the query.
$bind_id = ldap_bind($connect_id);
$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);
//Echo Connection Error
echo "Could not connect to LDAP server: $LDAP_SERVER[$SERVER_ID]";
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.
ldap_search() function. $search_id is created and is our search link identifier.
ldap_get_entries(). This will allow us to sort the information in a logical
manner for display.