note 93793 deleted from function.ldap-search by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 11:17:11 +0000 |
| Subject: | note 93793 deleted from function.ldap-search by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197074@lists.php.net to get a copy of this message | ||
Note Submitter: john dot saterfiel at act dot org
----
Here's an example of how to connect to an active directory (2003 windows ad tested). And
retrieve a specific person's information when given only their username (sAMAccountName).
Note: I have noticed that some active directory servers do care about what case the attributes are
in and others don't. This site was very useful in looking up the AD attributes but I did have
to lowercase them all to work for me.
Do a google search on "active directory person attributes" to find the site.
www.computerperformance.co.uk
I doubt the code below will work for everybody as is but it should give ad users a good start in how
to get a logged in user's data without knowing what organization or security group they're
in. This was needed because my company didn't put all their users in the same organization.
<?PHP
$ldap_url = 'examplead.mycomp.com';
$ldap_domain = 'mycomp.com';
$ldap_dn = "dc=mycomp,dc=com";
$ds = ldap_connect( $ldap_url );
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
$username = "your_user_name_here";
//must always check that password length > 0
$password = "your_password_here";
// now try a real login
$login = ldap_bind( $ds, "$username@$ldap_domain", $password );
echo '- Logged In Successfully<br/><br/>';
try{
$attributes = array("displayname", "mail",
"department",
"title",
"physicaldeliveryofficename",
"manager");
$filter = "(&(objectCategory=person)(sAMAccountName=$username))";
$result = ldap_search($ds, $ldap_dn, $filter, $attributes);
$entries = ldap_get_entries($ds, $result);
if($entries["count"] > 0){
//echo print_r($entries[$i],1)."<br />";
echo "<b>User Information:</b><br/>";
echo "displayName: ".$entries[0]['displayname'][0]."<br/>";
echo "email: ".$entries[0]['mail'][0]."<br/>";
echo "department: ".$entries[0]['department'][0]."<br/>";
echo "title: ".$entries[0]['title'][0]."<br/>";
echo "office:
".$entries[0]['physicaldeliveryofficename'][0]."<br/>";
//echo "manager: ".$entries[$i]['manager'][0]."<br/>";
$manager_result = ldap_search($ds,
$entries[0]['manager'][0],
'(objectCategory=person)',
array("displayname"));
$manager_entries = ldap_get_entries($ds, $manager_result);
if($manager_entries["count"] > 0){
echo "manager: ". $manager_entries[0]['displayname'][0];
}
}
}catch(Exception $e){
ldap_unbind($ds);
return;
}
ldap_unbind($ds);
echo '<br/><br/>- Logged Out';
?>