I want to do a simple ajax search for LDAP (or AD) using PHP and jQuery. I read several tutorials that are looking for mysql, for example:
http://www.codeforest.net/simple-search-with-php-jquery-and-mysql
I want to replace the mysql connection with my ldap connection details below. Can someone help me in my work, as I can’t get the results for the show.
thank
LDAP connection script
<?php echo "<?xml version='1.0' encoding='utf-8' ?>" ?><?php echo "<ul class='LSRes'>" ?>
<?php
if( isset($_GET['q']) &&!empty($_GET['q']) ){
$host = "10.10.10.10";
$user = "DOMAIN\user";
$pswd = "password";
$ad = ldap_connect($host)
or die( "Could not connect!" );
ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3)
or die ("Could not set ldap protocol");
$bd = ldap_bind($ad, $user, $pswd)
or die ("Could not bind");
$dn = "OU=accounts,DC=domain,DC=com";
$attrs = array("displayname","mail","samaccountname","telephonenumber","givenname", "title");
$filter = "(|(givenName=".$_GET['q']."*) (sn=".$_GET['q']."*) (displayname=".$_GET['q']."*) (samaccountname=".$_GET['q']."*))";
$search = ldap_search($ad, $dn, $filter, $attrs)
or die ("<span class='LSstyle_noresults'><strong>Could not connect to AD</strong></span>");
ldap_sort ( $ad, $search, 'sn' ) ;
$entries = ldap_get_entries($ad, $search);
if ($entries["count"] > 0) {
for ($i=0; $i<$entries["count"]; $i++) {
echo "<span class='LSstyle'>Name: <strong><a href=\"mailto:".$entries[$i]["mail"][0]."\">".$entries[$i]["displayname"][0]." ".$entries[$i]["sn"][0]."</a></strong></span><br />";
echo "<span class='LSstyle'>Short name: <strong>".$entries[$i]["samaccountname"][0]."</strong></span><br />";
echo "<span class='LSstyle'>Phone: <strong>".$entries[$i]["telephonenumber"][0]."</strong></span><br />";
echo "<span class='LSstyle'>Title: <strong>".$entries[$i]["title"][0]."</strong></span><br />";
echo "<span class='LSstyle'>Dept: <strong>".$entries[$i]["department"][0]."</strong></span></p>";
}
} else {
echo "<span class='LSstyle_noresults'><strong>No results found</strong></span>";
}
ldap_unbind($ad);
}
?>
source
share