Ross
Ross

Reputation: 173

PHP LDAP Error Handing (Undefined Index)

The app I'm working on right now involves an external library to interface with an LDAP server to retrieve data about users. I'm running into an issue where an index that I requested isn't defined for certain users. I know why it's undefined - that's normal. I need help figuring out how to handle it appropriately and not have the script die.

Here's an example where the error occurs. I'm trying to pull basic data (first/last name, phone number, and address) from LDAP. If a user doesn't have one of the attributes defined that I requested (say phone number), the script dies with Undefined index: phone. The other attributes are retrieved successfully, however.

How can I make it so that the script returns null for anything that's undefined in the response from the LDAP server instead of it crashing?

I've posted the relevant method from my library in PasteBin here.

Upvotes: 0

Views: 428

Answers (1)

Lirux
Lirux

Reputation: 169

You can verify first if the var is set before you assign it to $attribute_array.

$attribute_array = isset($found[$index][$attribute_key]) ? $found[$index][$attribute_key] : null;

You can edit null to whatever you want.

Upvotes: 1

Related Questions