Reputation: 315
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.
Upvotes: 1
Views: 56
Reputation: 24549
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!";
}
}
Upvotes: 1