Add license keys to a PHP app

To add license keys to a PHP app, run composer require licers/licers, create a LicenseClient with your public key, and call validateCached(). It asks the license server at most once an hour, checks the Ed25519 signature every time, and keeps your customer’s site running through an outage.

Updated by the Licers team

Set it up

  1. Create a free account and add a product.
  2. Copy your public key and product id from the Integration page. The public key is safe to ship inside your app.
  3. Issue a key: set how many devices it may use, when it expires, and any entitlements.
  4. Add the code below. On the first check the device is bound to the key; past the device limit a new machine is refused.
Terminal
composer require licers/licers
license.php
<?phprequire __DIR__ . '/vendor/autoload.php'; use Licers\LicenseClient;use Licers\LicenseException;use Licers\NetworkException; $client = new LicenseClient('YOUR_PUBLIC_KEY', [   // safe to ship inside your app    'product_id' => 'YOUR_PRODUCT_ID',   // your product: refuses keys for your other products    'app_version' => '2.4.1',   // shows in Analytics -> App versions]);$key = 'XXXX-XXXX-XXXX-XXXX';   // the customer's license key try {    $result = $client->validate($key);} catch (NetworkException $e) {    // Offline, or the server is down. That is not "unlicensed": try again, or    // allow a few days since the last good check (validateCached does that).    echo 'Could not reach the license server. Please try again.', PHP_EOL;    exit(1);} catch (LicenseException $e) {    // A forged or replayed answer, or a key for another of your products.    echo 'License check failed: ', $e->getMessage(), PHP_EOL;    exit(1);} if (!$result->valid) {    // Expired, revoked, device limit reached... the server says which.    echo 'Not licensed: ', $result->error, PHP_EOL;    exit(1);} echo 'Licensed. Expires: ', $result->expiresAt ?? 'never', PHP_EOL;

Websites: check without slowing every page

In PHP every request starts from nothing, so a plain check would call the license server on each page view. validateCached() stores the signed answer instead and checks its signature again on each request.

bootstrap.php
<?phprequire __DIR__ . '/vendor/autoload.php'; use Licers\Device;use Licers\LicenseClient;use Licers\NetworkException; $client = new LicenseClient('YOUR_PUBLIC_KEY', [    'product_id' => 'YOUR_PRODUCT_ID',    'hwid' => Device::site('https://customer-site.example'),   // one website is one device]); try {    // Asks the server at most once an hour. In an outage it keeps going on the last    // signed yes for three days. The file is signed: editing it can't make a license.    $result = $client->validateCached($key, __DIR__ . '/../storage/license.json', 3600, 3 * 86400);    $licensed = $result->valid;} catch (NetworkException $e) {    $licensed = false;   // no answer, and no recent one either: decide what your app allows}
  • Editing the stored file, copying it to another site, or setting the clock back does not make a license.
  • A refusal is remembered for five minutes and an outage for one, so a busy site never waits on every request.
  • Use one file per key, and put it outside the web root.

What is a device in PHP?

  • A website: Device::site($url). The dashboard shows the domain. Upper case, www., the port and the query make no difference.
  • A command-line tool or a server of the customer: the default, Device::id(). It is the same id the Node, .NET, Java, Rust, Go and C SDKs use.
  • Shared hosting: use the website. All customers of a host have the same computer.

Errors

validate returns a result with valid === false when the server refuses the key. Everything else is an exception, and all of them extend LicenseException.

What happenedWhat your app should do
The key is expired, revoked, blocked or over its device limitShow the message the server sent. You can reword every one of them on the Custom Messages page.
The answer has a bad signature or an old nonceRefuse. Somebody is faking the server.
The server cannot be reached, or returns a 5xx errorRetry, or allow a grace period since the last good check. Being offline is not the same as being unlicensed.

Entitlements, credits, variables, seats and offline files

Entitlements. Switch features on per key: true/false, numbers or text. They come from the signed answer.

entitlements.php
<?phprequire __DIR__ . '/vendor/autoload.php'; use Licers\LicenseClient; $client = new LicenseClient('YOUR_PUBLIC_KEY', [   // safe to ship inside your app    'product_id' => 'YOUR_PRODUCT_ID',   // your product: refuses keys for your other products    'app_version' => '2.4.1',   // shows in Analytics -> App versions]);$key = 'XXXX-XXXX-XXXX-XXXX';   // the customer's license key $result = $client->validate($key);   // first: this activates the device on the keyif (!$result->valid) {    echo 'Not licensed: ', $result->error, PHP_EOL;    exit(1);} // Entitlements of this key, for example ['pro' => true, 'seats' => 5, 'tier' => 'gold'].// They are read from the signed copy, so nobody can edit them on the way.if ($result->feature('pro') === true) {    echo 'Pro features unlocked', PHP_EOL;} $seats = $result->feature('seats', 1);        // 1 when the key has no "seats"$tier = $result->feature('tier', 'basic');echo 'Seats: ', $seats, ' Tier: ', $tier, ' All: ', json_encode($result->features), PHP_EOL;

Offline file. Check a signed .lic file with no internet at all, for air-gapped machines. Signature, product, expiry, device and operating system are all checked on the computer.

offline.php
<?phprequire __DIR__ . '/vendor/autoload.php'; use Licers\Device;use Licers\LicenseClient;use Licers\LicenseException;use Licers\SignatureException; // 1. Show this to the customer. They send it to you, and you create the file//    for that device: License keys -> ... -> Offline license file.echo 'Device id: ', Device::id(), PHP_EOL; // 2. The customer saves the file next to your app. No network is used here.try {    $license = LicenseClient::verifyOffline('YOUR_PUBLIC_KEY', 'license.lic', [        'product_id' => 'YOUR_PRODUCT_ID',   // your product    ]);} catch (SignatureException $e) {    echo 'The license file is missing, damaged or was edited.', PHP_EOL;    exit(1);} catch (LicenseException $e) {    echo 'License file refused: ', $e->getMessage(), PHP_EOL;   // expired, other device, OS or product    exit(1);} // 3. Use it. It never returns an invalid license: it throws instead.echo 'Licensed to: ', $license->customer, ' Expires (unix time): ', $license->expiresAt ?? 'never', PHP_EOL;if ($license->feature('pro') === true) {    echo 'Pro features unlocked', PHP_EOL;}

Usage credits. Meter what the customer does: “100 exports”, “5,000 AI calls a month”. Spending is all or nothing.

usage.php
<?phprequire __DIR__ . '/vendor/autoload.php'; use Licers\LicenseClient;use Licers\UsageExceededException; $client = new LicenseClient('YOUR_PUBLIC_KEY', [   // safe to ship inside your app    'product_id' => 'YOUR_PRODUCT_ID',   // your product: refuses keys for your other products    'app_version' => '2.4.1',   // shows in Analytics -> App versions]);$key = 'XXXX-XXXX-XXXX-XXXX';   // the customer's license key $result = $client->validate($key);   // first: this activates the device on the keyif (!$result->valid) {    echo 'Not licensed: ', $result->error, PHP_EOL;    exit(1);} echo 'Left: ', $client->getUsage($key)->remaining ?? 'no limit', PHP_EOL;   // reads only try {    $usage = $client->recordUsage($key, 1);   // spend 1 credit, all or nothing    echo 'Done. ', $usage->remaining ?? 'Unlimited', ' credits left', PHP_EOL;} catch (UsageExceededException $e) {    echo 'Out of credits. Refill: ', $e->usage['resets_at'] ?? 'buy more', PHP_EOL;}

Remote variables. Values you change in the dashboard without shipping an update: a link, a message, a switch.

variables.php
<?phprequire __DIR__ . '/vendor/autoload.php'; use Licers\LicenseClient; $client = new LicenseClient('YOUR_PUBLIC_KEY', [   // safe to ship inside your app    'product_id' => 'YOUR_PRODUCT_ID',   // your product: refuses keys for your other products    'app_version' => '2.4.1',   // shows in Analytics -> App versions]);$key = 'XXXX-XXXX-XXXX-XXXX';   // the customer's license key $result = $client->validate($key);   // first: this activates the device on the keyif (!$result->valid) {    echo 'Not licensed: ', $result->error, PHP_EOL;    exit(1);} // Values you set on the Remote Variables page. Signed, and only for valid keys.$url = $client->getVariable($key, 'download_url');       // null when it isn't set$theme = $client->getVariable($key, 'theme', 'dark');    // or your own default$everything = $client->getVariables($key);               // all this key may see echo $url, ' ', $theme, ' ', json_encode($everything, JSON_UNESCAPED_SLASHES), PHP_EOL;

Floating seats. Let only N people run the app at the same time, on any computers.

floating.php
<?phprequire __DIR__ . '/vendor/autoload.php'; use Licers\LicenseClient;use Licers\SeatUnavailableException; $client = new LicenseClient('YOUR_PUBLIC_KEY', [   // safe to ship inside your app    'product_id' => 'YOUR_PRODUCT_ID',   // your product: refuses keys for your other products    'app_version' => '2.4.1',   // shows in Analytics -> App versions]);$key = 'XXXX-XXXX-XXXX-XXXX';   // the customer's license key try {    $seat = $client->session($key);    echo 'Seat taken: ', $seat->seatsUsed, ' of ', $seat->seatsMax, PHP_EOL;     foreach (['job 1', 'job 2'] as $job) {        // ... your work runs here. Call this at least once a minute: it sends the        // heartbeat when one is due, and throws once the seat is gone.        $seat->keepAlive();    }     $seat->stop();   // frees the seat for someone else} catch (SeatUnavailableException $e) {    echo 'No seat: ', $e->getMessage(), PHP_EOL;}

Updates. Tell the customer about a new version. Only a valid, active key is given the download link.

updates.php
<?phprequire __DIR__ . '/vendor/autoload.php'; use Licers\LicenseClient; $client = new LicenseClient('YOUR_PUBLIC_KEY', [   // safe to ship inside your app    'product_id' => 'YOUR_PRODUCT_ID',   // your product: refuses keys for your other products    'app_version' => '2.4.1',   // shows in Analytics -> App versions]);$key = 'XXXX-XXXX-XXXX-XXXX';   // the customer's license key $update = $client->checkUpdate($key, '2.4.1', 'stable');   // or 'beta' if ($update->updateAvailable) {    echo 'New version: ', $update->version, PHP_EOL;    echo "What's new: ", $update->notes, PHP_EOL;    // The answer is signed, but downloadUrl and notes are not: check the URL is    // on your own host, and verify the file against a hash you publish.    echo 'Download: ', $update->downloadUrl, PHP_EOL;} else {    echo 'You have the latest version.', PHP_EOL;}

Questions and answers

Does it work with Laravel or Symfony?

Yes. It is plain PHP with no dependencies. Bind one LicenseClient in your container and call validateCached() with a file in your storage folder.

Can a customer just remove the check?

PHP is delivered as source, so someone who edits your code can. Signatures stop fake servers and edited files, not edited code. Tie what customers pay for to your server too: updates, downloads, cloud features.

Which PHP versions and extensions?

PHP 7.4 or newer with sodium, which is part of PHP. cURL is used when it is there. Tested on PHP 8.2.

Can I use it without Composer?

Yes. Copy the folder and require its autoload.php.

What does it cost?

Nothing today. Every feature is free and unlimited: products, keys, devices, validations, offline files and floating seats. See pricing.