471,336 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

Sorting Active Directory/LDAP Output (Lastname, Firstname)

Hello Everyone,

My company has asked me to put a company directory on our intranet site
and I'm trying to use php to extract the users from our active
directory server. I've got everything working, however, when the list
of users is shown in the output, it seems to display the users in the
order their accounts were created, with Administrator obviously being
first. What do you think would be the best way to get the list to be
sorted by lastname (sn), based on the coding below. usort? ldap_sort?
Not really too familiar with using php and ldap or active directory.

Any help would be appreciated, thank you!

<?php
$ldap_server = "ldap://ourldap";
$auth_user = "user";
$auth_pass = "pass";

// Set the base dn to search the entire directory.

$base_dn = "ou=staff, dc=ourldap, dc=com";

// Show People
$filter = "(&(objectClass=user)(objectCategory=person)(cn=*) )";
// 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]["name"][0]."<br>";
echo "Name: ". $info[$i]["sn"][0].", ".
$info[$i]["givenname"][0]."<br>";
echo "Email: ". $info[$i]["mail"][0]."<br>";
echo "Title: ". $info[$i]["description"][0]."<br>";
echo "Ext.: ". $info[$i]["telephonenumber"][0]."<br>";
echo "Website.: ". $info[$i]["wwwhomepage"][0]."<p>";

}

ldap_close($connect);
?>

Jul 17 '05 #1
2 5335
The way I would do it is probably not the most efficient way but, you
could make $item a global variable and then use a custom sort so for
example:

first define this function in the php file:

function sortbysn($x, $y) {
global $item;
$xsn = $item[$x]['sn'][0];
$ysn = $item[$x]['sn'][0];
return ($xsn < $ysn? 1: -1);
}

and then to access the sort by:

uksort($item, 'sortbysn');

Let's see what everyone else comes up with though. Hope mine helps. I
never used LDAP so they're might be an easier way.

PS: Shouldn't use double quotes when you're not evaluating variables in
a string. For example, "My name is $name" is okay but "My name is
Belmin" is an inefficient way of using double quotes since there's no
variable in there. 'My name is Belmin' is more efficient (you use double
quotes in key names for your arrays).

Written by "Victor Lokhmatov" on 2/23/05 2:02p:
Hello Everyone,

My company has asked me to put a company directory on our intranet site
and I'm trying to use php to extract the users from our active
directory server. I've got everything working, however, when the list
of users is shown in the output, it seems to display the users in the
order their accounts were created, with Administrator obviously being
first. What do you think would be the best way to get the list to be
sorted by lastname (sn), based on the coding below. usort? ldap_sort?
Not really too familiar with using php and ldap or active directory.

Any help would be appreciated, thank you!

<?php
$ldap_server = "ldap://ourldap";
$auth_user = "user";
$auth_pass = "pass";

// Set the base dn to search the entire directory.

$base_dn = "ou=staff, dc=ourldap, dc=com";

// Show People
$filter = "(&(objectClass=user)(objectCategory=person)(cn=*) )";
// 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]["name"][0]."<br>";
echo "Name: ". $info[$i]["sn"][0].", ".
$info[$i]["givenname"][0]."<br>";
echo "Email: ". $info[$i]["mail"][0]."<br>";
echo "Title: ". $info[$i]["description"][0]."<br>";
echo "Ext.: ". $info[$i]["telephonenumber"][0]."<br>";
echo "Website.: ". $info[$i]["wwwhomepage"][0]."<p>";

}

ldap_close($connect);
?>


--

Belmin Fernandez

Visit: http://www.belminfernandez.com/homepage
Email: belminf at gmail period com
Jul 17 '05 #2
Good job so far with the LDAP. Looks so easy.

Can you try putting this up as a service in the .NET VSN realm, then
make a WSDL call to get your lists?

The reason I suggest it is that it's likely the next thing you'll be
asked to do is to change the user's passwords. LDAP to AD over port
686 SSL for such secure operations is a nightmare. LDAP is so touchy
and always breaking, if you can get it to work at all, and the
support out there for AD/LDAP is lousy I've found. Might be easier to
do that stuff on the Windows box and talk to the deployed service with
a WSDL from PHP.

Go ahead, show them how smart the PHP folks are...

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ted Vreeland via DotNetMonster.com | last post: by
1 post views Thread by Christopher | last post: by
reply views Thread by Martijn | last post: by
2 posts views Thread by Jim in Arizona | last post: by
1 post views Thread by Carlettus | last post: by
4 posts views Thread by Jon | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.