473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 5499
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: huzz | last post by:
I have web application that quaries the Active Directory to get user details.. everything works fine but someday I'll get System.Runtime.InteropServices.COMExection and if I restart the client...
2
by: Ted Vreeland via DotNetMonster.com | last post by:
Hi Folks, I've got a intranet ASP.NET page that takes a user id (e.g. 'DoeJ') passed in and attempts to get the First and Last name of the user from the Active Directory. I'm running into some...
1
by: Christopher | last post by:
We are using forms authentication in our web app and typically query our LDAP Servers by binding to the user node in the LDAP Tree. We usually see the following DN used as the DN for each user.....
0
by: Martijn | last post by:
Hello, For those who are interested in creating Account in Active Directory here is some code I used. ublic Function create_user(ByVal firstname As String, ByVal lastname As String, _ ByVal...
2
by: Jim in Arizona | last post by:
My goal, somehow, is to populate a dropdownlist with all the user names in active directory. I don't even know where to begin, really. I added a reference to System.DirectoryServices so I could...
1
kenobewan
by: kenobewan | last post by:
Hi there, having a problem getting a complete users list out of AD. Code used in component: oRoot = New DirectoryEntry("LDAP://LDAPServer/DC=domain,DC=com") oSearcher =...
1
by: Carlettus | last post by:
Dear All, sorry but I'm not sure if this is the right place to post my problem. I was using the following asp code to create users in Active Directory. Suddenly, and I don't know the reason, users...
0
by: rbukkara | last post by:
Hi, I have got the following error while trying to add a user in the LDAP Directory. javax.naming.NameNotFoundException: ; remaining name 'uid=vassila,ou=People,dc=cs,dc=uno,dc=edu' I have...
4
by: Jon | last post by:
I am modifying an app for a customer in ASP.Net 1.1. The app is running on a server outside their network, yet they want to authenticate users against their internal active directory set up (they...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.