Core\Ldap\Changes::cookie
Returns the cookie of a sync, which you give to the next call of searchChanges.
A sync with searchChanges returns the entries that changed since the last run. The cookie is how the server knows when the last run was. Read it after the loop, and store it, for example in a database. The next run gives it back with the cookie option and gets only the entries that changed since. The first run has no cookie, so it returns every matching entry.
The cookie is bytes. To store it as text, convert it with Core\Encoding::toBase64.
Good to know: if the loop stops early, the cookie covers only the entries it read. The next run returns the other entries again, so nothing is lost. The cookie is tainted, because the server sent it.
Return value
Returns tainted bytes. The cookie. It is tainted, because the server sent it.
Errors
LogicErrorThe connection is closed.
Examples
The examples below run with this configuration file.
# These examples use the LDAP directory server on this computer at port 16636.# `tls_ca_file` is the certificate that the server's certificate is checked against.[ldap.corp]url = "ldaps://127.0.0.1:16636"base = "DC=example,DC=test"user = "CN=Reader,OU=Company,DC=example,DC=test"password = "Novis-reader-1"tls_ca_file = "../../../../../tests/db/ca.crt"timeout = "5s"
# The program may open the `corp` directory.[capabilities.ldap]connect = ["corp"]The first run
<?nvs// Shows the first run of a sync: it returns every matching entry, and then a cookie.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');
// There is no cookie yet, so the result has every person in Sales.var $sales = $corp->searchChanges(Core\Ldap\Filter::equals('department', 'Sales'), { select: ['cn'],});
array<tainted string> $names = [];foreach ($sales as Core\Ldap\Entry $person) { $names[] = $person->dn()->toString();}// The server sends the entries in no fixed order, so they are sorted.// This prints the DNs of Anna Berg and Carla Diaz.foreach (Core\Arr::sort($names) as tainted string $name) { echo $name, "\n";}
// After the loop, `cookie` returns the cookie for the next run. It is not empty.echo Core\Bytes::length($sales->cookie()) > 0 ? "Keep the cookie for the next run.\n" : "No cookie.\n";CN=Anna Berg,OU=Company,DC=example,DC=testCN=Carla Diaz,OU=Company,DC=example,DC=testKeep the cookie for the next run.Store the cookie as text
<?nvs// Shows how to store the cookie as text, and use it in the next run.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $sales = Core\Ldap\Filter::equals('department', 'Sales');
// The first run reads every person in Sales.var $first = $corp->searchChanges($sales, { select: ['cn'] });foreach ($first as Core\Ldap\Entry $person) {}
// The cookie is bytes. `toBase64` makes text from it, which you can save// in a database or a file.tainted string $saved = Core\Encoding::toBase64($first->cookie());
// The next run starts from the saved cookie. Nobody changed a person in// Sales in between, so this prints 0.var $next = $corp->searchChanges($sales, { select: ['cn'], cookie: Core\Encoding::fromBase64($saved) });int $changed = 0;foreach ($next as Core\Ldap\Entry $person) { $changed = $changed + 1;}echo $changed, "\n";0A loop that stops early
<?nvs// Shows what the cookie covers when a loop stops before the last entry.// requires: samba-ad
var $corp = Core\Ldap::connect('corp');var $sales = Core\Ldap\Filter::equals('department', 'Sales');
// This loop stops after the first person.var $first = $corp->searchChanges($sales, { select: ['cn'] });foreach ($first as Core\Ldap\Entry $person) { break;}
// The cookie covers only what the loop read. So the next run returns// the people the loop did not read. This prints a number above 0.var $next = $corp->searchChanges($sales, { select: ['cn'], cookie: $first->cookie() });int $again = 0;foreach ($next as Core\Ldap\Entry $person) { $again = $again + 1;}echo $again > 0 ? "Some people are returned again.\n" : "Nobody is returned again.\n";Some people are returned again.