Sorting Active Directory/LDAP Output (Lastname, Firstname)

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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!


$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].", ".
   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>";



Re: Sorting Active Directory/LDAP Output (Lastname, Firstname)

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...

Re: Sorting Active Directory/LDAP Output (Lastname, Firstname)

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

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:
Quoted text here. Click to load it


Belmin Fernandez

Email: belminf at gmail period com

Site Timeline