Jump to content

Recommended Posts

Hello all! I'm having an issue where I'm trying to query LDAP, however it's failing and returning -

Warning: ldap_get_attributes() expects parameter 2 to be resource, array given in C:\wamp\www\ad.php on line 34.

 

This is running on a WAMP server for use on an internal LAN on a Windows machine.

Here's the code

<?php 
$dn = "DC=ad,DC=fakedomain,DC=com"; 
if (isset($_POST['userid'])){
    $userid = rtrim($_POST['userid']); 
}

if (isset($_POST['criteria'])){
    $cri = rtrim($_POST['criteria']); 
}
    $login = "username@ad.fakedomain.com"; 
    $password = "password"; 

    $ad = ldap_connect("ad.fakedomain.com") 
          or die("Couldn't connect to AD!"); 
    ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3); 
    ldap_set_option($ad, LDAP_OPT_REFERRALS, 0); 
          
    $bind = ldap_bind($ad,$login,$password);
    $filter = "($cri=$userid)"; 
    $result = ldap_search($ad,$dn,$filter);
echo $result."<br />";
    $entry = ldap_get_entries($ad,$result);
if (!isset($entry)){
echo "Your query has failed."; }
else {
echo $entry;
$info = ldap_get_attributes($ad,$entry);

for ($i=0; $i<$info["count"]; $i++) {
    $desc = ($info[$i]);
    $detail = ldap_get_values($ad,$entry,$desc);

   echo $info[$i]." </td><td align=left>";
   for ($a=0; $a < $detail["count"]; $a++) {
   echo $detail[$a];
     $b = $a + 1;
} ;};};
    ldap_unbind($ad);
    ?>

 

Can anyone tell me why this is failing? I am a PHP newb, but definitely interested. Thanks!

Link to comment
https://forums.phpfreaks.com/topic/186751-failing-to-query-ldap/
Share on other sites

Warning: ldap_get_attributes() expects parameter 2 to be resource, array given in C:\wamp\www\ad.php on line 34.

 

The second parameter you're giving ldap_get_attributes() is $entry.  Now $entry is obviously an array returned by the method  ldap_get_entries().  You need to change $entry to $result which is a resource returned by ldap_search().

Just to verify we're on the same page, is this what you're saying? Unfortunately I won't be in a position to check until tomorrow, but I am eager. I am slowly discovering the awesomeness that is PHP and you'll prolly be seeing me a bit more in the forums. :D

 

$filter = "($cri=$userid)";     
$entry = ldap_get_entries($ad,$filter);
$result = ldap_search($ad,$dn,$entry);

 

 

I'm not sure about this, but I have been experimenting with PHP/LDAP the last two weeks and in the beginning I had some weird error messages because the connection and/or bind did not succeed.

 

You might want to try to add 'or die()'-statements to your connection / bind-cmd's.

 

Also: I had to set my ldap-options even before the connection was made (you set it between the connection and the bind)...

 

HTH

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.