473,604 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)(objectCa tegory=person)( cn=*))";
// connect to server

if (!($connect=@ld ap_connect($lda p_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=@lda p_search($conne ct, $base_dn, $filter))) {
die("Unable to search ldap server");
}

$number_returne d = ldap_count_entr ies($connect,$s earch);
$info = ldap_get_entrie s($connect, $search);
echo "The number of entries returned is ". $number_returne d."<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]["descriptio n"][0]."<br>";
echo "Ext.: ". $info[$i]["telephonenumbe r"][0]."<br>";
echo "Website.: ". $info[$i]["wwwhomepag e"][0]."<p>";

}

ldap_close($con nect);
?>

Jul 17 '05 #1
2 5518
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)(objectCa tegory=person)( cn=*))";
// connect to server

if (!($connect=@ld ap_connect($lda p_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=@lda p_search($conne ct, $base_dn, $filter))) {
die("Unable to search ldap server");
}

$number_returne d = ldap_count_entr ies($connect,$s earch);
$info = ldap_get_entrie s($connect, $search);
echo "The number of entries returned is ". $number_returne d."<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]["descriptio n"][0]."<br>";
echo "Ext.: ". $info[$i]["telephonenumbe r"][0]."<br>";
echo "Website.: ". $info[$i]["wwwhomepag e"][0]."<p>";

}

ldap_close($con nect);
?>


--

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
5425
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 machine then it works again. here is one of the method where am calling the AD public bool UserExist(string UserName) {
2
1763
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 issues and am hoping someone can look at it and tell me whats wrong. I've gotten several different errors (Referal from server, unspecified error, etc). Any assistance would be greatly appreciated. Best Regards, Ted CODE: -----------------...
1
2943
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.. http://serverip:389/cn=login,dc=company,dc=org *where login is their actual windows login In Active Directory we are seeing this format... http://server:389/cn=firstname lastname,dc=company,dc=org
0
1200
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 Address As String, ByVal pwd As String) Try Dim MyRoot As DirectoryServices.DirectoryEntry
2
5958
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 use the System.DirectoryServices.ActiveDirectory namespace. I don't even know if this is the right way to go as I can't seem to find anything in that namespace that would help me query active directory for names. I can't use an LDAP query...
1
1182
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 = New DirectorySearcher(oRoot) Dim strFilter As String strFilter += "(&" strFilter += "(objectCategory=CN=Person,CN=Schema,CN=Configuration,DC=rayaust,DC=au,DC=ray,DC=com)"
1
1961
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 are created but the account is disabled (see the flag User.AccountDisabled = False ). There is also another problem even if the user does not exist , the application returns to me with the message that the user already exist. Thank you for...
0
3210
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 given all the attributes which are needed, for the user, in the code and also the proper path where the user has to be added. Please have a look at my code CODE] // This is a class file which stores all the info required for the user
4
2275
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 will open the necessary ports). So, I have a simple login page with username and password, and then I will authenticate that credentials entered against their AD server. I am having a real hard time figuring this out. We can't use Windows Forms...
0
7997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8409
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8065
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3907
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3955
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1526
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.