Link to home
Start Free TrialLog in
Avatar of adznon
adznon

asked on

PHP LDAP Lookup

I have a PHP webpage that captures the username of the person connecting to it.

I now need to do a lookup in Active Directory to get the email address (SecurityPincipal.sAMAccountName)

I have had some success with the below code, in it will bind successfully with LDAP but does not return anything

<?php

//using ldap bind anonymously

// connect to ldap server
$ldapconn = ldap_connect("global.tesco.org")
    or die("Could not connect to LDAP server.");

if ($ldapconn) {

    // binding anonymously
    $ldapbind = ldap_bind($ldapconn);

    if ($ldapbind) {
        echo "LDAP bind anonymous successful...";

        ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION,3);
        ldap_set_option($ldapconn, LDAP_OPT_REFERRALS,0);

        $dn = "CN=Users,DC=name,DC=name,DC=org"; 
        $filter="(sAMAccountName=username)";
        $justthese = array("ou", "sn", "givenname", "mail");

        $sr=ldap_search($ldapconn, $dn, $filter, $justthese);

        $info = ldap_get_entries($ldapconn, $sr);

        echo $info["count"]." entries returned\n";

    } else {
        echo "LDAP bind anonymous failed...";
    }
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial