View Full Version : Get Display Name From Active Directory

01-31-2012, 06:23 AM
Hey All,

I have been trying for a little while to figure this out and just can'r wrap my head around it.

I have some code for querying an Active Directory server in PHP to get a users display name.

The code is...

$ldap_server = "server.domain.com:389";
$auth_user = "username@domain.com";
$auth_pass = "password";

$base_dn = "OU=MyBusiness,OU=Users,OU=SBSUsers,DC=domain,DC=com";
$filter = "(&(objectCategory=user)(memberOf=IM-ALL_USERS))";

// connect to server
if (!($connect=@ldap_connect($ldap_server))) {
die("Could not connect to ldap server");

// bind to server
if (!($bind = ldap_bind($connect, $auth_user, $auth_pass))) {
die("Unable to bind to server");

// search active directory
if (!($search = ldap_search($connect, $base_dn, $filter))) {
die("Unable to search ldap server");

$number_returned = ldap_count_entries($connect,$search);
$info = ldap_get_entries($connect, $search);

echo "The number of entries returned is ". $number_returned."<p>";

for ($i=0; $i<$info["count"]; $i++) {
echo "Name is: ". $info[$i]["givenname"][0]."<br>";
echo "Display name is: ". $info[$i]["displayname"][0]."<br>";
echo "Email is: ". $info[$i]["mail"][0]."<br>";
echo "Telephone number is: ". $info[$i]["telephonenumber"][0]."<p>";

The way I understand it this code should loop through active directory and output the details specified for each user but it doesn't. Instead I get an error which says "No Such Object". We are running SBS 2003 and when I access AD manager and navigate through the structure it goes


to access the users list for our domain.

Additionally other than displaying a list of all the users like this how would I modify this script once working so that I can pass a username and get the Active Directory display name for a user.

Thanks in advance guys.