472,993 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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 5462
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.