Skip to content

Core\Ldap\Filter::equals

Builds a filter that finds the directory entries where an attribute has exactly this value.

You give the attribute's name, such as sAMAccountName, and the value to look for. Pass the filter to search on a connection to get the entries it matches. The value is sent to the server as data. A *, ( or ) in it is a normal character, so a value from a form cannot change what the filter finds.

The attribute's name must be written in your program, because it is not allowed to be tainted. A name with a character that an attribute name cannot have throws a LogicError.

Good to know: the server decides if upper and lower case count. In Active Directory, most text attributes ignore case, so anna also finds Anna.

Parameters

  • $attributestringThe attribute's name, such as sAMAccountName. It cannot be tainted.
  • $valuestringThe value to match. It may be tainted, such as a login name from a form. A * or ) in it is a normal character.

Return value

Returns Core\Ldap\Filter. A Core\Ldap\Filter.

Errors

  • LogicError$attribute is not an attribute name.

Examples

Find an account by login name

<?nvs
// Shows how to build a filter that finds one account by its login name.
var $filter = Core\Ldap\Filter::equals('sAMAccountName', 'anna');
// `toString` shows the filter as text. This prints (sAMAccountName=anna).
echo $filter->toString(), "\n";
Output
(sAMAccountName=anna)

A star is a normal character

<?nvs
// Shows that a value with a `*` finds only that exact value.
// Think of this text as a login name that somebody typed into a form.
string $typed = 'ann*';
var $filter = Core\Ldap\Filter::equals('sAMAccountName', $typed);
// The `*` is part of the value and not a wildcard, so it is written as `\2a`.
// This prints (sAMAccountName=ann\2a).
echo $filter->toString(), "\n";
Output
(sAMAccountName=ann\2a)

A name that is not an attribute

<?nvs
// Shows what happens when the attribute name has a character it cannot have.
try {
// An attribute name cannot contain a space.
Core\Ldap\Filter::equals('login name', 'anna');
} catch (LogicError $error) {
echo "Not an attribute name.\n";
}
// A number is a correct attribute name too: 2.5.4.3 is the number of `cn`.
echo Core\Ldap\Filter::equals('2.5.4.3', 'Anna Lee')->toString(), "\n";
Output
Not an attribute name.
(2.5.4.3=Anna Lee)