Jump to content

[SOLVED] LDAP and PHP - Undefined Index?


wwfc_barmy_army

Recommended Posts

I'm using php to display users from the LDAP and i'm trying to output their usernames, so i am using this piece of code:

    for ($i=0; $i < $entries["count"]; $i++)

    {

        echo $entries[$i]["sAMAccountName"][0]."<br />";

    }

 

Although this produces the following error for each user:

Notice: Undefined index: sAMAccountName in C:\xampp\htdocs\test4.php on line 45

 

Using 'displayname' works:

 

    for ($i=0; $i < $entries["count"]; $i++)

    {

        echo $entries[$i]["displayname"][0]."<br />";

    }

 

 

Anyone know what is causing this and how i could solve it?

 

Thanks.

Link to comment
Share on other sites

Well, first, you should really be using a "foreach" loop, not a "for" loop (also, please use the code button for listing code):

<?php
    foreach ($entries["count"] as $entry)
    {
        echo $entry["sAMAccountName"][0]."<br />";
    }
?>

Second, your error is saying that the element with key "sAMAccountName" does not exist. To check what's in your array, do this before and look at the output:

<?php
print_r($entries);
?>

Link to comment
Share on other sites

Sorry, I missed this part. You had $entries["count"] and it should have been count($entries). That threw me off. Should have been this:

<?php
    foreach ($entries as $entry)
    {
        echo $entry["sAMAccountName"][0]."<br />";
    }
?>

Link to comment
Share on other sites

No, the original poster had it correct. LDAP returns data in a crazy format.

 

That NOTICE means that key doesn't exist. Are you sure you spelled it correct? Remember it's case sensitive. You can debug by using the following to see the key/value pairs:

 

print_r($entries[0]);

Link to comment
Share on other sites

No, the original poster had it correct. LDAP returns data in a crazy format.

 

That NOTICE means that key doesn't exist. Are you sure you spelled it correct? Remember it's case sensitive. You can debug by using the following to see the key/value pairs:

 

print_r($entries[0]);

 

Thanks, from this i found out it was all lowercase. Thanks again!

Link to comment
Share on other sites

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.