Forum Moderators: coopster

Message Too Old, No Replies

Parallel LDAP search?

Search ldap with two multiple DNs

         

urbanmac

7:53 pm on Jan 17, 2011 (gmt 0)

10+ Year Member




I have an php ldap search script but that returns results but I want it to return results from two different sources (DN) at the same time.

I believe this is possible by doing a parallel search but I can't figure it out. My current script looks like this but is not returning results from both DNs, any help appreciated.


<?php echo "<?xml version='1.0' encoding='utf-8' ?>" ?><?php echo "<ul class='LSRes'>" ?>
<?php
if( isset($_GET['q']) &&!empty($_GET['q']) ){
// all your ldap code

// Designate a few variables
$host = "10.10.10.10"; // Add in your AD host name or IP
$user = "DOMAIN\user"; // Add in your AD access account user name
$pswd = "password"; // Add in your AD access account user name password

$ds = 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($ds, $user, $pswd)
or die ("Could not bind");

// Create the DN - Add in the OU of your AD
$dn[] = "OU=uk,OU=Accounts,DC=mywebsite,DC=com";
$dn[] = "OU=us,OU=Accounts,DC=mywebsite,DC=com";

$id[] = $ds;
$id[] = $ds;


//$filter = 'samaccountname='.$_POST['username'];
$filter = "(|(givenName=".$_GET['q']."*) (sn=".$_GET['q']."*) (displayname=".$_GET['q']."*) (samaccountname=".$_GET['q']."*))";



$result = ldap_search($id,$dn,$filter);

$search = false;

foreach ($result as $value) {
if(ldap_count_entries($ds,$value)>0){
$search = $value;
break;
}
}

if($search){
$entries = ldap_get_entries($ds, $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);
}
?>

coopster

3:14 pm on Feb 3, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Resolved yet? Can you check the LDAP logs to see what, if any, messages you are receiving? Because you can indeed search multiple DNs.

urbanmac

4:29 pm on Feb 3, 2011 (gmt 0)

10+ Year Member



Not resolved yet.

coopster

5:22 pm on Feb 4, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Is it returning anything at all? You need to turn up error_reporting [php.net] during development. Your ldap_search [php.net] function should be failing because you are using invalid arguments by the looks of it. $id should not be an array, it should be the ldap connection resource, $ds. And $dn should not be of type array, it should be a string.

urbanmac

9:35 am on Feb 7, 2011 (gmt 0)

10+ Year Member



Hi,

what it does is rather odd.

First user search uses first OU entry
Second user search used second OU entry
Third user search uses first OU entry
.....and this continues, swaps between each OU.