Hi,
I've been using a PHP script to query an AD/LDAP server and retrieve people information. The search criteria (filter) works by selecting either First Name, Surname, Phone Number from a drop down list and then entering a search term.
EG: Select Firstname from drop down enter 'Ma' into the search field and anyone in the AD/LDAP Firstname beginning with 'Ma' is returned, eg Martha, Margaret etc.
What I'd like to do and have been trying to do is remove the drop down and then make the field you enter the search term in to search across all the options that are dropdown box at the same time.
Therefore if I search for 'Ma' not only do I get Martha, Margaret etc. I also get Macallister, Malloy etc. (surnames)
But I'm stumped on how to go about this. Heres my AD search:
<form action="adsearch.php" method="post">
Search criteria:<br />
<input type="text" name="keyword" size="20"
maxlength="20" value="" /><br />
Filter:<br />
<select name="filter">
<option value="sn">Choose One:</option>
<option value="sn">Last Name</option>
<option value="telephonenumber">Phone</option>
<option value="givenname">First Name</option>
<option value="mail">Email Address</option>
</select><br />
<input type="submit" value="Search!" />
</form>
</p>
<?php
// Designate a few variables
$host = "HOSTNAME";
$user = "USER";
$pswd = "PASSWORD";
$ad = ldap_connect($host)
or die( "Could not connect!" );
// Set version number
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3)
or die ("Could not set ldap protocol");
// Binding to ldap server
$bd = ldap_bind($ad, $user, $pswd)
or die ("Could not bind");
// Create the DN
$dn = "OU=USERS,DC=DOMAIN,DC=COM";
// Specify only those parameters we're interested in displaying
$attrs = array("displayname","mail","samaccountname","telephonenumber","givenname");
// Create the filter from the search parameters
$filter = $POST['filter']."=".$POST['keyword']."*";
$search = ldap_search($ad, $dn, $filter, $attrs)
or die ("");
$entries = ldap_get_entries($ad, $search);
if ($entries["count"] > 0) {
for ($i=0; $i<$entries["count"]; $i++) {
echo "<p>Name: ".$entries[$i]["displayname"][0]."<br />";
echo "Email: <a href=mailto:".$entries[$i]["mail"][0].">".$entries[$i]["mail"][0]."</a><br />";
echo "Short name: ".$entries[$i]["samaccountname"][0]."<br />";
echo "Phone: ".$entries[$i]["telephonenumber"][0]."</p>";
}
} else {
echo "<p>No results found!</p>";
}
ldap_unbind($ad);
?>
[The script above is from a developer.com article]