I am creating a login page that uses Active Directory credentials. I found some documentation about LDAP in the PHP Manual and managed to successfully login using Active Directory credentials by binding with LDAP.
My challenge is that I have multiple Organizational Units (OU) and can't find an efficient way to bind using OUs.
Currently I am using a for loop to loop over each OU in an array. However, this throws an error each time the incorrect OU is being used. I am new to LDAP and found resources that mentioned ldap_search; however, I was not able to successfully bind without including the OU. Any advice/ help would be great.
$ou=array("Group1", "Group2", "Group3", "Group4", "Group5");
$arrlength = count($ou);
for($x = 0; $x < $arrlength; $x++){
$ldapuser = "CN=".$_POST["username"].",OU=".$ou[$x].",DC=com,DC=com";
$ldappass = trim($_POST["password"]);
//LDAP server connection
$ldapserver = "ldaps:server.domain";
//options are require, never, allow
//require is most secure, the others could allow for man in the middle attacks
putenv('LDAPTLS_REQCERT=require');
// connect to ldap server
$ldapconn = ldap_connect($ldapserver) or die ("Couldn't connect");
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
// binding to ldap server
$ldapbind = false;
$ldapbind = ldap_bind($ldapconn, $ldapuser, $ldappass);
if ($ldapbind) {
$x = $arrlength;
}