How to Implement Software Licensing in PHP

If you sell PHP applications or software that needs to be restricted to licensed customers, you need a way to verify licenses and enforce the corresponding entitlements in your code.

For a simple application, this can mean checking whether a license key is valid before allowing access to a particular function. As the product grows, licensing may also need to handle subscription periods, feature entitlements, activations, and offline environments.

This guide shows how to implement software licensing in PHP using Devolens, including the basic code required to activate and verify a license.

What Does Software Licensing Code for PHP Need to Do?

At its simplest, software licensing code checks whether a customer has a valid license before allowing the application or a particular feature to run.

The licensing flow typically looks like this:

  1. The customer receives a license key.
  2. Your PHP application receives the key.
  3. The application sends the key and relevant product information to the licensing system.
  4. The licensing system verifies the license.
  5. Your application uses the result to determine what the customer can access.

The important part is that the licensing check becomes part of your application's normal execution flow. For example, you might check a license before enabling a premium feature rather than simply checking once when the application is installed.

For a broader explanation of how licensing systems work, see our guide to how software licensing works.

‍

Using a PHP Licensing SDK

You can implement the licensing API directly in PHP, but using an SDK avoids having to build the API integration and activation logic yourself.

Devolens provides a PHP library that can be used to activate license keys from PHP applications. The current PHP library supports license key activation through the Devolens API.

The basic setup is straightforward. Download the PHP library and include it in your application:

<?php
require_once('Cryptolens.php');

You then need the values that identify your licensing setup:

  • An access token with permission to activate licenses
  • The product ID
  • The license key supplied to the customer
  • A machine code, if your licensing model uses machine binding

The access token and product ID are associated with your Devolens account and product. The license key normally comes from the customer or from the provisioning system that delivers licenses to your application.

Implement License Verification in PHP

Once the library is included, you can activate a license using the cryptolens_activate function.

A basic example looks like this:

<?php

require_once('Cryptolens.php');

$activate = cryptolens_activate(
   // Access token
   'YOUR_ACCESS_TOKEN',
   // Product ID
   1234,
   // License Key
   'AAAAA-BBBBB-CCCCC-DDDDD',
   // Machine Code
   'YOUR_MACHINE_CODE'
);

if ($activate) {
   echo "License is valid.";
} else {
   echo "License is not valid.";
}

?>

The example uses placeholder values deliberately. Your access token, product ID, license key, and machine code should come from your own licensing setup rather than being copied from an example.

The PHP library returns a boolean indicating whether the activation attempt was successful.

For more detailed implementation examples and the parameters available to the API, refer to the software licensing implementation guide.

‍

Enforce Licenses in Your PHP Application

Activating a license is only one part of implementing software licensing. Your application also needs to use the result of the license check to control access.

For example, you could check the license before allowing a customer to use a premium function:

if (!$activate) {
   exit("A valid license is required to use this feature.");
}

// Premium functionality
runPremiumFeature();

A more useful implementation will usually distinguish between different entitlements rather than treating the license as simply valid or invalid.

For example, you might have:

  • A basic license with access to a limited set of features
  • A professional license with additional functionality
  • A subscription license that expires at a defined date
  • Different activation limits for different customers

This keeps the licensing logic connected to the commercial model of your product. Your application decides what the customer can use based on the license information rather than maintaining a separate set of access rules.

Get Insights on Software Licensing In Production

Thank you for subscribing to the Devolens newsletter.
Something went wrong while submitting the form. Please try again later.

Choosing a Machine Code for PHP Applications

Machine binding can be useful when you want to restrict a license to a particular server or installation.

The PHP library does not calculate the machine code itself, so your application needs to provide an appropriate value when activation is performed.

The value you use depends on how your PHP application is deployed. For example, a server based application may use an identifier associated with the installation rather than relying on a particular hardware component.

This is worth considering carefully before implementing it. If the identifier changes during a normal server migration or reinstallation, legitimate customers may unexpectedly lose access to their license.

‍

Handling Licensing in Production

A production licensing implementation usually needs to account for more than the initial activation request.

For example, you may need to decide:

  • How often the application checks the license
  • What happens when the licensing service cannot be reached
  • How subscription expiry is enforced
  • How different license features map to application functionality
  • How licenses are reactivated after a server migration
  • Whether customers need to use the application in an offline environment

These decisions are part of the licensing architecture rather than PHP specific code.

If your application needs to work without an internet connection, Devolens supports offline licensing using signed license files.

‍

A Practical Approach to PHP Software Licensing

For a small PHP application, the basic implementation can be relatively short. The more important decision is how that initial license check fits into the rest of the application.

A useful implementation separates licensing from the business logic wherever possible. Your application can retrieve the license state once and then use that information to determine which functionality should be available.

This makes it easier to change licensing models later without spreading licensing checks throughout the entire codebase.

If you are implementing a complete licensing system rather than adding a single license check, it is also worth considering how license creation, activation, renewals, expiry, and entitlement changes will be handled. These are the parts that tend to create ongoing maintenance work when licensing is implemented entirely inside the application.

‍

Conclusion

Implementing software licensing in PHP does not require a large amount of application code. The core flow is to obtain a license key, activate it through the licensing system, and use the result to enforce the appropriate access rules in your application.

The implementation can then be extended to support different licensing models, feature entitlements, machine binding, and offline environments as your product requires.

With the basic licensing layer separated from the rest of your PHP application, you can change how licenses are issued and managed without having to redesign the application's core functionality.

2018-10-17

Devolens - Modern Software Licensing Infrastructure

Learn more about Our Product
Learn more about Our Product

Devolens - Modern Software Licensing Infrastructure

View more Tutorials
View more Tutorials

Get Started with Devolens

Deploy licensing with a leading software licensing provider without long implementation cycles or added operational overhead.