PHP LDAP library functionality packed in a simple class
1. Installation
Run in your project directory:
composer require stepansib/ldap
2. Usage
1. LDAP object
To configure connection instantiate LDAP object with connection params array:
use StepanSib\LDAP\LDAP;
$ldap = new LDAP2(array(
'host' => 'server.company.com',
'username' => 'johndoe',
'password' => '12345',
'domain' => 'Company',
'base_dn' => 'DC=Company,DC=com',
'anonymous' => false, // Optional parameter
'can_login_via_shortname' => true, // Optional parameter, set to false if your LDAP instance requires authentication by full qualified name
));
2. Connection
To connect and bind to LDAP:
$ldap->connect();
Usage of this method is not necessary because connection will be established automatically when it will needed
3. User authentication
To authenticate any account:
$ldap->authenticate("jdoe", "12345");
$ldap->authenticate("johndoe@company.com", "12345");
$ldap->authenticate("John Doe", "12345");
The username credential can be any of following LDAP entry parameters:
- cn
- displayname
- name
- sAMAccountName
4. Search
To find any record in LDAP directory use the following search method. You need to specify filter string, baseDN string and array of parameters you want to get per entry:
$filter = '(memberOf=CN=Company Management,CN=Users,DC=Company,DC=com)';
//$filter = '&(objectClass=user)(sAMAccountName=jdoe))'; // any valid filter can be passed
$baseDn = 'DC=Company,DC=com';
$data = $ldap->search(
$filter,
array(
'cn',
'distinguishedname',
'displayname',
'department',
'title',
'sAMAccountName',
'mail',
'displayName'
),
$baseDn,
);
The method returns assoc array with matched entries and their parameters values
5. Close connection
After things was done you need to close connection:
$ldap->close();