Skip to content

Core\Ldap\Dn::child

Returns the DN of an entry directly below this one. The new part, attribute=value, is added at the start.

Use it to build the DN of a new user or group below a base you know, such as OU=Staff,DC=example,DC=test. The value can come from user input. Each special character in it is escaped, so it stays one value.

Good to know: the DN you call it on does not change, and you get a new DN back. An empty value throws a LogicError, and so does an attribute that is not a name.

Parameters

  • $attributestringThe attribute's name, such as CN or OU. It cannot be tainted.
  • $valuestringThe value, such as Ann Lee. It can be tainted. A ,, +, = or other special character in it is escaped, so it stays one value.

Return value

Returns Core\Ldap\Dn. A new Core\Ldap\Dn. This DN does not change.

Errors

  • LogicErrorThe attribute is not an attribute name, or the value is empty.

Examples

Build a path

<?nvs
// Shows how to build a DN one part at a time, from the top down.
var $staff = Core\Ldap\Dn::of('DC', 'test')
->child('DC', 'example')
->child('OU', 'Staff');
// This prints OU=Staff,DC=example,DC=test.
echo $staff->toString(), "\n";
Output
OU=Staff,DC=example,DC=test

A new user

<?nvs
// Shows how to make the DN of a new user from a name a person typed.
var $staff = Core\Ldap\Dn::parse('OU=Staff,DC=example,DC=test');
// This text came from a form, so it is `tainted`.
tainted string $name = 'Ann+uid=7';
var $user = $staff->child('CN', $name);
// The `+` and the `=` are escaped, so the name is still one value.
// This prints CN=Ann\+uid\=7,OU=Staff,DC=example,DC=test.
echo $user->toString(), "\n";
Output
CN=Ann\+uid\=7,OU=Staff,DC=example,DC=test

The base stays the same

<?nvs
// Shows that `child` returns a new DN, and the DN you call it on does not change.
var $staff = Core\Ldap\Dn::parse('OU=Staff,DC=example,DC=test');
var $ann = $staff->child('CN', 'Ann');
var $ben = $staff->child('CN', 'Ben');
// This prints CN=Ann,OU=Staff,DC=example,DC=test.
echo $ann->toString(), "\n";
// This prints CN=Ben,OU=Staff,DC=example,DC=test.
echo $ben->toString(), "\n";
// This prints OU=Staff,DC=example,DC=test.
echo $staff->toString(), "\n";
Output
CN=Ann,OU=Staff,DC=example,DC=test
CN=Ben,OU=Staff,DC=example,DC=test
OU=Staff,DC=example,DC=test