Skip to content

Core\Ldap\Sid::parse

Reads SID text, such as S-1-5-32-544, and returns a Core\Ldap\Sid. A SID (security identifier) is the number Windows uses for a user, a group or a computer.

Use it for a SID that you wrote yourself or that a user typed. Then you can compare it with the SIDs a directory returns, or read its parts with domain and rid.

Good to know: the text starts with S-1-, then the authority, then 1 to 15 numbers. Each number is at most 4294967295. Text that is not a SID throws a LogicError, and its message says which part is wrong.

Parameters

  • $textstringThe text. It starts with S-1-, then the authority, then 1 to 15 numbers, all joined by -.

Return value

Returns Core\Ldap\Sid. A Core\Ldap\Sid.

Errors

  • LogicErrorThe text is not a SID. The message says which part is wrong.

Examples

Read a sid

<?nvs
// Shows how to read SID text and get its last number.
var $admins = Core\Ldap\Sid::parse('S-1-5-32-544');
// This prints S-1-5-32-544 and then 544.
echo $admins->toString(), "\n";
echo $admins->rid(), "\n";
Output
S-1-5-32-544
544

Check the text

<?nvs
// Shows how to check text that should be a SID.
array<string> $texts = ['S-1-5-32-544', 'S-1-5-admins', 'S-1-5'];
// The first text is a SID. The other two throw a `LogicError`.
foreach ($texts as string $text) {
try {
echo Core\Ldap\Sid::parse($text)->toString(), " is a SID\n";
} catch (LogicError $refusal) {
echo $text, " is not a SID\n";
}
}
Output
S-1-5-32-544 is a SID
S-1-5-admins is not a SID
S-1-5 is not a SID

Compare two sids

<?nvs
// Shows that two ways to write the same SID give the same SID.
// The authority may be written in hex with `0x`. Both texts have authority 5.
var $short = Core\Ldap\Sid::parse('S-1-5-18');
var $long = Core\Ldap\Sid::parse('S-1-0x000000000005-18');
// The bytes are the same, so this prints same.
echo $short->bytes() == $long->bytes() ? 'same' : 'different', "\n";
Output
same