Performing AD (Active Directory) query with PHP

Asked

Viewed 1,485 times

1

Currently I have in here in the environment the AD (Active Directory) from Windows Server 2012. Today in my PHP application I can already connect to this AD via ldap_connect + ldap_bind.

But I would like to hold a consultation within the AD. Search for example the displayname field, mail or any attribute contained in the Attribute Editor tab.

I’ve already located in the PHP manual ldap_search, but I couldn’t make it work properly. Not to mention that I don’t know if this function would be the right one for my case.

Thank you.

EDIT-- Date:27/04/2015.

I managed to perform the query, see how was my code:

$filtro = "(&(objectClass=user)(objectCategory=person)(displayname=*)(sAMAccountname=$login1))";
$mostrar = array("samaccountname");
if (!($busca=@ldap_search($connect, $base_dn, $filtro, $mostrar))){
    die("Não foi possível realizar busca no Active Directory");
}
$info = ldap_get_entries($connect, $busca);
echo "<pre>".print_r($info,true)."</pre>";exit;
  • 1

    I am not going to put it as an answer, but you will find it in the English version of http://stackoverflow.com/questions/9471771/getting-the-users-full-name-from-active-directory-using-php

  • Thanks user3632930, helped a lot, but I don’t know if I’m doing something wrong, but I’m getting all AD users, I just wanted the user information that is logging in! Look at my code as it is: http://pastebin.com/7iEFjdrS

1 answer

1


I managed to perform the query, see how was my code:

$filtro = "(&(objectClass=user)(objectCategory=person)(displayname=*)(sAMAccountname=$login1))";
$mostrar = array("samaccountname");
if (!($busca=@ldap_search($connect, $base_dn, $filtro, $mostrar))){
    die("Não foi possível realizar busca no Active Directory");
}
$info = ldap_get_entries($connect, $busca);
echo "<pre>".print_r($info,true)."</pre>";exit;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.