note 93793 added to function.ldap-search

From: Date: Tue, 29 Sep 2009 17:10:47 +0000
Subject: note 93793 added to function.ldap-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-161153@lists.php.net to get a copy of this message
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'; ?> ---- Server IP: 208.69.120.35 Probable Submitter: 151.148.122.100 ---- Manual Page -- http://www.php.net/manual/en/function.ldap-search.php Edit -- https://master.php.net/note/edit/93793 Del: integrated -- https://master.php.net/note/delete/93793/integrated Del: useless -- https://master.php.net/note/delete/93793/useless Del: bad code -- https://master.php.net/note/delete/93793/bad+code Del: spam -- https://master.php.net/note/delete/93793/spam Del: non-english -- https://master.php.net/note/delete/93793/non-english Del: in docs -- https://master.php.net/note/delete/93793/in+docs Del: other reasons-- https://master.php.net/note/delete/93793 Reject -- https://master.php.net/note/reject/93793 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#161153) next »