How can you do an Active Directory lookup via PHP? Without needing to recompile PHP. PHP version is 5.3
I want to find a persons display name from their user name. Web server is IIS 6 and PHP is served using FastCGI.
I get the username with:
$cred = explode('\\',$_SERVER['REMOTE_USER']);
if (count($cred) == 1) array_unshift($cred, "(no domain info - perhaps SSPIOmitDomain is On)");
list($domain, $user) = $cred;
return $user;
So how can I then find the name? e.g. DoeJ = John Doe
Edit:
Trying to lookup user, but not sure how to find the "base DN". Don't have direct access to the Active Directory server or have admin rights, so connecting anonymously.
<?php
//using ldap bind anonymously
// connect to ldap server
$ldapconn = ldap_connect("example.co.uk")
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"; // also tried DC=example,DC=co,DC=uk
$filter="(SAMAccountName=username)";
$justthese = array("ou", "sn", "givenname", "mail");
$sr=ldap_search($ldapconn, $dn, $filter, $justthese);
$info = ldap_get_entries($ds, $sr);
echo $info["count"]." entries returned\n";
} else {
echo "LDAP bind anonymous failed...";
}
}
?>
Fails on the ldap_search: Warning: ldap_search() [function.ldap-search]: Search: Operations error