Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4081376
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:03:10+00:00 2026-05-20T18:03:10+00:00

I’m trying to create a PHP script that will return some details of each

  • 0

I’m trying to create a PHP script that will return some details of each member that is part of a specific group in our Active Directory.

I have no problem connecting and display the names (CN) of the group members but when it comes to displaying details such as telephone, email and username I’m stuck.

Here’s my code I’m trying with. Can anyone see what I’m doing wrong?

<?php
$ldap_server = "AD_Server.domain.pri:389";
$auth_user = "user@domain.pri";
$auth_pass = "password";

$base_dn = "OU=IM Groups,OU=GLOBAL,DC=domain,DC=pri";
$filter = "(&(objectCategory=user)(memberOf=IM-ALL_USERS))";

// 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]["givenname"][0]."<br>";
   echo "Display name is: ". $info[$i]["displayname"][0]."<br>";
   echo "Email is: ". $info[$i]["mail"][0]."<br>";
   echo "Telephone number is: ". $info[$i]["telephonenumber"][0]."<p>";
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:03:11+00:00Added an answer on May 20, 2026 at 6:03 pm

    Worked it out using an excellent function created by Sam J Levy.

    Here’s the final code that worked.

    <?php
    
    function explode_dn($dn, $with_attributes=0)
    {
        $result = ldap_explode_dn($dn, $with_attributes);
        foreach($result as $key => $value) $result[$key] = preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $value);
        return $result;
    }
    
    function get_members($group,$user,$password) {
        $ldap_host = "LDAPSERVER";
        $ldap_dn = "OU=some_group,OU=some_group,DC=company,DC=com";
        $base_dn = "DC=company,DC=com";
        $ldap_usr_dom = "@company.com";
        $ldap = ldap_connect($ldap_host);
    
        ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION,3);
        ldap_set_option($ldap, LDAP_OPT_REFERRALS,0);
    
        ldap_bind($ldap, $user . $ldap_usr_dom, $password);
        $results = ldap_search($ldap,$ldap_dn, "cn=" . $group);
        $member_list = ldap_get_entries($ldap, $results);
    
        $dirty = 0;
        $group_member_details = array();
    
        foreach($member_list[0]['member'] as $member) {
            if($dirty == 0) {
                $dirty = 1;
            } else {
                $member_dn = explode_dn($member);
                $member_cn = str_replace("CN=","",$member_dn[0]);
                $member_search = ldap_search($ldap, $base_dn, "(CN=" . $member_cn . ")");
                $member_details = ldap_get_entries($ldap, $member_search);
                $group_member_details[] = array($member_details[0]['givenname'][0],$member_details[0]['sn'][0],$member_details[0]['telephonenumber'][0],$member_details[0]['othertelephone'][0]);
            }
        }
        ldap_close($ldap);
        return $group_member_details;
    }
    
    // Specify the group from where to get members and a username and password with rights to query it
    $result = get_members("groupname","username","password");
    
    // The following will create an XML file with the details from $group_member_details
    $xml = simplexml_load_string("<?xml version='1.0'?>\n<AddressBook></AddressBook>");
    $version = $xml->addChild('version', '1');
    
    foreach($result as $e) {
        $contact = $xml->addChild('Contact');
        $contact->addChild('FirstName', $e[0]);
        $contact->addChild('LastName', $e[1]);
        $phone = $contact->addChild('Phone');
        if ($e[3] == '') {
                    $phone->addChild('phonenumber', '0');
            } else {
                    $phone->addChild('phonenumber', $e[3]);
            }
        $phone->addChild('accountindex', '0');
        $phone = $contact->addChild('Phone');
        if ($e[2] == '') {
            $phone->addChild('phonenumber', '0');
        } else {
            $phone->addChild('phonenumber', $e[2]);
        }
        $phone->addChild('accountindex', '1');
        $contact->addChild('Group', '0');
        $contact->addChild('PhotoUrl', 'empty');
    }
    
    $xml->asXML('phonebook.xml');
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.