Avatar of AshcorTech
AshcorTech
 asked on

PHP LDAP query to Active Directory

Hello all,

Prior to this I've never had a need to interact with AD so I'm very new to it.

I have a website tool written in PHP5, running on a Linux Apache server.  This site had it's own login functionality and I've recently replaced that with querying the Active Directory domain for authentication using LDAP (borrowing from code examples found online).

this works fine.

Step 2) limiting access to only users in a certain AD security group however, is proving difficult.  The problem I'm having is constructing the AD query sting to use.  

If I look on the Windows Srv 2008 domain controller in the Active Directory Users and Computers tool I can find the group in this tree...

xxx.yyy.com > Organizational Units > CompanyName > groups and distribution lists > Security Groups > Support

So my question is, how do write the PHP LDAP query string to search that security group for a specific username.  

so for instance if the user login is "bsmith" I want so search that security group for any object with CN=bsmith I believe.

so I believe, to get a list of all users in that group, the query should look something like:

$query="(memberOf=OU=CompanyName,CN=Support,DC=xxx,DC=yyy,DC=com)";
$ldap_dn="OU=CompanyName,DC=xxx,DC=yyy,DC=com";

$results = ldap_search($ldap,$ldap_dn,$query);
if(!$results)echo "ERROR: ".ldap_error($ldap)."<BR>";

Open in new window


returns:

"Error: No such object"

any help with this would be much appreciated!

thanks.
PHPActive DirectorySQL

Avatar of undefined
Last Comment
AshcorTech

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Joseph Daly

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
AshcorTech

ASKER
xxdcmast,

thanks for the suggestion.  I did that and came up with this query but still the same results..

(&(memberOf=CN=Support,OU=Security Groups,OU=Groups and Distribution Lists,OU=CompanyName,OU=Organizational Units,DC=xxx,DC=yyy,DC=com))

Open in new window


:(
AshcorTech

ASKER
xxdcmast,

scratch that last post, I re-read what you wrote and changed:

$ldap_dn = "OU=CompanyName,OU=Organizational nits,DC=xxx,DC=yyy,DC=com";  

Open in new window



and now I'm getting results.  

Many thanks!!!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck