LDAPClient is a PHP library designed to make LDAP operations easy and flexible. From basic queries to advanced LDAP functionalities, this library offers a wide range of features.
- Simplified LDAP Connection
- User Authentication
- Advanced Search Queries
- Debugging Support
composer require faulkj/ldapclient
Initiate a connection to the LDAP server.
$server
: The LDAP server URL$bindDN
: The DN to bind with$pass
: The password for binding$baseDN
: The base DN for the directory$options
: List of LDAP fields to pull. Allows for custom attribute mappings to align with the specific LDAP system you're working with.
Example:
use FaulkJ\LDAPClient
$ldap = new LDAPClient(
"ldap.forumsys.com",
"cn=read-only-admin,dc=example,dc=com",
"password",
"dc=example,dc=com",
[
"id" => "uid"
]
);
Toggle debugging output for LDAP operations. When enabled, this provides verbose output for troubleshooting.
$dbg
: Boolean to enable or disable debugging
Example:
$ldap->debug(true);
Returns:
- Current debug state if no parameter is provided
- The LDAPClient object itself, allowing for method chaining, if a parameter is provided
Converts the search result to a JSON format.
$filter
: LDAP filter$attr
: Attributes to fetch$dn
: Optional DN to search under$resolveDNs
: Whether to resolve DNs$stayBound
: Whether to stay bound
Example:
die($ldap->getJSON("(ou=chemist*)", ["cn", "members" => "uniquemember"], null, true));
Returns: JSON-formatted string representing the search results.
Authenticate a user and return their attributes.
$id
: User ID$pass
: Password$attr
: Attributes to fetch
Example:
if($user = $ldap->login("riemann", "password", ["fullname" => "cn", "mail"])) {
echo "{$user->fullname} successfully logged in!";
}
else echo "Invalid username or password!";
Returns:
- User object with specified attributes if successful
false
if authentication failsnull
if the user is not found
Check if a user is a member of a specific LDAP group or groups.
$user
: The user ID to check$group
: Group or array of groups to check membership$options
: Optional array for additional attribute mappings (merges with default options)
Example:
if($ldap->member("mathematicians", "gauss", [
"id" => "ou",
"member" => "uniquemember"
])) {
echo "gauss is a mathematician!";
}
Returns:
true
if the user is a memberfalse
if the user is not a membernull
if the user is not found
Retrieves the photo for a specified user.
Example:
$ldap->photo("simmons");
Returns: Outputs the image directly to the screen if available, a blank image if not
Perform an LDAP search.
$filter
: LDAP filter$attr
: Attributes to fetch$dn
: Optional DN to search under$resolveDNs
: Whether to resolve DNs$stayBound
: Whether to stay bound
Example:
$res = $ldap->search(
"(ou=chemist*)",
[
"cn",
"members" => "uniquemember"
],
null,
true
);
echo implode("\n", $res->members);
Returns:
- Array of LDAP records if multiple records are found
- Single LDAP record if only one is found
- null if no records are found