1

I have what looks like a rather strange array, to me anyway, this is the result of a print_r ($info) and of var_export; :

print_r ($info);

output

Array ( [count] => 1 [0] => 
Array ( [sessiontimeout] => Array ( [count] => 1 [0] => 0 ) [0] => sessiontimeout [description] => Array ( [count] => 1 [0] => Admin group ) [1] => description [cn] => Array ( [count] => 1 [0] => ldap-admin ) [2] => cn [objectclass] => Array ( [count] => 2 [0] => top [1] => GroupV2 ) [3] => objectclass [enabled] => Array ( [count] => 1 [0] => TRUE ) [4] => enabled [uniquemember] => Array ( [count] => 2 [0] => uid=username1,cn=users,dc=abc,dc=net [1] => uid=username2,cn=users,dc=abc,dc=net )
[5] => uniquemember [count] => 6 [dn] => cn=ldap-admin,cn=staff,cn=company,cn=groups,dc=abc,dc=net ) )
var_export($info);
array ( 'count' => 1, 0 => array ( 'sessiontimeout' => array ( 'count' => 1, 0 => '0', ), 0 => 'sessiontimeout', 'description' => array ( 'count' => 1, 0 => 'Admin group', ), 1 => 'description', 'cn' => array ( 'count' => 1, 0 => 'ldap-admin', ), 2 => 'cn', 'objectclass' => array ( 'count' => 2, 0 => 'top', 1 => 'oGroupV2', ), 3 => 'objectclass', 'enabled' => array ( 'count' => 1, 0 => 'TRUE', ), 4 => 'enabled', 'uniquemember' => array ( 'count' => 2, 0 => 'uid=username1,cn=users,dc=jhc,dc=net', 1 => 'uid=username2,cn=users,dc=abc,dc=net', ), 5 => 'uniquemember', 'count' => 6, 'dn' => 'cn=ldap-admin,cn=staff,cn=company,cn=groups,dc=abc,dc=net', ), )

im only interested in the uniquemember uid= parts. I have been trying to set a variable that is uid=usernameX and then check if the variable contains the username i have been given previously in my code. It looks like this:

for ($i=0; $i<=$info["count"]; $i++) {
  $uid = $info[$i]["uniquemember"][$i];

  if (strpos($info, "uid=$username")) {
    echo "$username  valid";
  } else {
    echo $username not valid";
  }
}

As far as i can tell my variable uid is not being reset to the second instance of uid=, in this case it should be "uid=username2"

If the username provided is username1 i get the output

username1 valid

If the username is username2 i get

username2 not valid

When i would expect it to be valid.

4
  • What is $info[$]? Did you mean to put a variable in there? Commented Nov 25, 2019 at 13:52
  • $i<=$info["count"] should probably be only < without the = Commented Nov 25, 2019 at 13:53
  • var_export($info) into your question might facilitate reproducing the issue Commented Nov 25, 2019 at 14:02
  • @JeremyHarris typo - edited to fix. -dWinder Thats what i had to start but it didnt make a difference. -jibsteroos looks very similar to print_r output but ive added to question as well Commented Nov 25, 2019 at 14:06

1 Answer 1

1

It may be useful if you break it up into smaller parts so you can more clearly see what is happening. One thing to note is that strpos can return 0 if it matches on the first char (which it will) and the way you are writing the if statement, its only checking for truthy or falsy. Change it to use !== false for a proper comparison.

// Get unique members array
$uniqueMembers = $info[0]['uniquemember'];

// Get unique member count
$memberCount = (int) $uniqueMembers['count'];

// Loop through members
for ($i = 0; $i < $memberCount; $i++) {

    // Get the UID string
    $uid = $uniqueMembers[$i];

    // Check if it matches the supplied username
    if (strpos($uid, "uid=$username") !== false) {
       echo "$username is valid!";
    } else {
       echo "$username is not valid!";
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @jeremy, i think a dollar is missing in the $memberCount variable? Also it seems to work where the username matches $uid but because it loops through i am getting it also return $username is not valid when it compares the second username to the username given?
Ah, yep, fixed. You can simply add a break() in the condition where you find a valid user to exit the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.