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
https://forums.phpfreaks.com/topic/128858-solved-ldap-and-php-undefined-index/
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);
?>

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 />";
    }
?>

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]);

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.