note 107545 added to function.ldap-search

From: Date: Thu, 16 Feb 2012 15:52:47 +0000
Subject: note 107545 added to function.ldap-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-186169@lists.php.net to get a copy of this message
I wanted to look up some information from various fields of our Active Directory LDAP server. I thought I would contribute the results of that research here: <?php /************************************************** Bind to an Active Directory LDAP server and look something up. ***************************************************/ $SearchFor="fred"; //What string do you want to find? $SearchField="samaccountname"; //In what Active Directory field do you want to search for the string? $LDAPHost = "10.10.10.10"; //Your LDAP server DNS Name or IP Address $dn = "DC=whatever,DC=whatever"; //Put your Base DN here $LDAPUserDomain = "@something.something"; //Needs the @, but not always the same as the LDAP server domain $LDAPUser = "ldapuserid"; //A valid Active Directory login $LDAPUserPassword = "passforuser"; $LDAPFieldsToFind = array("cn", "givenname", "samaccountname", "homedirectory", "telephonenumber", "mail"); $cnx = ldap_connect($LDAPHost) or die("Could not connect to LDAP"); ldap_set_option($cnx, LDAP_OPT_PROTOCOL_VERSION, 3); //Set the LDAP Protocol used by your AD service ldap_set_option($cnx, LDAP_OPT_REFERRALS, 0); //This was necessary for my AD to do anything ldap_bind($cnx,$LDAPUser.$LDAPUserDomain,$LDAPUserPassword) or die("Could not bind to LDAP"); error_reporting (E_ALL ^ E_NOTICE); //Suppress some unnecessary messages $filter="($SearchField=$SearchFor*)"; //Wildcard is * Remove it if you want an exact match $sr=ldap_search($cnx, $dn, $filter, $LDAPFieldsToFind); $info = ldap_get_entries($cnx, $sr); for ($x=0; $x<$info["count"]; $x++) { $sam=$info[$x]['samaccountname'][0]; $giv=$info[$x]['givenname'][0]; $tel=$info[$x]['telephonenumber'][0]; $email=$info[$x]['mail'][0]; $nam=$info[$x]['cn'][0]; $dir=$info[$x]['homedirectory'][0]; $dir=strtolower($dir); $pos=strpos($dir,"home"); $pos=$pos+5; if (stristr($sam, "$SearchFor") && (strlen($dir) > 8)) { print "\nActive Directory says that:\n"; print "CN is: $nam \n"; print "SAMAccountName is: $sam \n"; print "Given Name is: $giv \n"; print "Telephone is: $tel \n"; print "Home Directory is: $dir \n"; } } if ($x==0) { print "Oops, $SearchField $SearchFor was not found. Please try again.\n"; } ?> ---- Server IP: 208.69.120.55 Probable Submitter: 204.98.2.180 ---- Manual Page -- http://www.php.net/manual/en/function.ldap-search.php Edit -- https://master.php.net/note/edit/107545 Del: integrated -- https://master.php.net/note/delete/107545/integrated Del: useless -- https://master.php.net/note/delete/107545/useless Del: bad code -- https://master.php.net/note/delete/107545/bad+code Del: spam -- https://master.php.net/note/delete/107545/spam Del: non-english -- https://master.php.net/note/delete/107545/non-english Del: in docs -- https://master.php.net/note/delete/107545/in+docs Del: other reasons-- https://master.php.net/note/delete/107545 Reject -- https://master.php.net/note/reject/107545 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186169) next »