Search the LDAP Directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | /******************************************************* * MYCPLUS Sample Code - https://www.mycplus.com * * * * This code is made available as a service to our * * visitors and is provided strictly for the * * purpose of illustration. * * * * Please direct all inquiries to saqib at mycplus.com * *******************************************************/ <html> <head> <title>ldap_search</title> </head> <body> <? /* ** Function: compareEntry ** This function compares two entries for ** the purpose of sorting. */ function compareEntry($left, $right) { $ln = strcmp($left["last"], $right["last"]); if($ln == 0) { return(strcmp($left["first"], $right["first"])); } else { return($ln); } } //connect to LDAP server if(!($ldap=ldap_connect("ldap.php.net"))) { die("Could not connect to LDAP server!"); } //set up search criteria $dn = "dc=php, dc=net"; $filter = "sn=Atkinson"; $attributes = array("givenname", "sn"); //perform search if(!($result = ldap_search($ldap, $dn, $filter, $attributes))) { die("Nothing Found!"); } //get all the entries $entry = ldap_get_entries($ldap, $result); print("There are " . $entry["count"] . " people.<br />\n"); //pull names out into array so we can sort them for($i=0; $i < $entry["count"]; $i++) { //Note how we only use the first entry. This //code assumes people only have one first name, //and one last name. $person[$i]["first"] = $entry[$i]["givenname"][0]; $person[$i]["last"] = $entry[$i]["sn"][0]; } //sort by last name, then first name using //compareEntry (defined above) usort($person, "compareEntry"); //loop over each entry for($i=0; $i < $entry["count"]; $i++) { print($person[$i]["first"] . " " . $person[$i]["last"] . "<br />\n"); } //free memory used by search ldap_free_result($result); ?> </body> </html> |