swett.bp swett.bp - 2 months ago 11
PHP Question

How can i get manager name from AD in php?

Now, i'm doing to connect my website to AD use ldap and get data from user logon but my problem is when user logon and i need to get manager name of user i get manager name by call

$entries[0]["manager"][0]
it's show data like this
CN=rosie,OU=AllUser,DC=xyz,DC=local
i need to get only manager name that is 'rosie' but i don't know to get it, please help

this my code

$ldap_host ="servername";
$ldap_port = "port";
$ldap_dn = "OU=AllUser,DC=abc,DC=local";
$ldap_usr_dom = '@abc.local';
$ldap_it_group = "IT";
$ldap = ldap_connect($ldap_host,$ldap_port) or die("Could not connect to ".$ldap_host);
$bind = @ldap_bind($ldap, $user.$ldap_usr_dom, $password) or die("cant bind");
if($bind) {
$filter = "(&(objectClass=*)(sAMAccountName=".$user."))";
$attr = array("manager");
$result = ldap_search($ldap, $ldap_dn, $filter, $attr) or exit("Unable to search LDAP server");
$entries = ldap_get_entries($ldap, $result);
ldap_unbind($ldap);
for ($i=0; $i<$entries["count"]; $i++) {
echo "Manager is: ". $entries[0]["manager"][0]."<p>";
}

Answer

A totally horrible way of doing it... doing regex would be better, but this is a quick hack job for ya, until the regex guys come around to provide the expression.

$manager = str_replace('CN=','',strstr($entries[0]["manager"][0],',',true));
echo "Manager is: ". $manager ."<p>";
Comments