PHP code example of nazrulalif / laravel-pem-license-validator

1. Go to this page and download the library: Download nazrulalif/laravel-pem-license-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nazrulalif / laravel-pem-license-validator example snippets


return [
    'license_path' => storage_path('app/license.pem'),

    'cache' => [
        'enabled' => true,
        'ttl' => 86400, // 24 hours
        'driver' => 'file',
    ],

    'grace_period_days' => 0, // 0=disabled, 7=7 days after expiry

    'product_key' => env('LICENSE_PRODUCT_KEY'), // Optional product validation

    'public_key' => env('LICENSE_PUBLIC_KEY', '-----BEGIN PUBLIC KEY-----...'),

    'on_failure' => [
        'redirect' => '/license-expired',
        'log' => true,
    ],
];

use Nazrulalif\LaravelPemLicenseValidator\LicenseValidator;

$validator = new LicenseValidator(config('license'));

if ($validator->isValid()) {
    $license = $validator->getLicenseData();
    echo "Valid until: " . $license['expiryDate'];
} else {
    abort(403, 'Invalid license');
}

use Nazrulalif\LaravelPemLicenseValidator\Facades\License;

if (License::isValid()) {
    $daysLeft = License::getDaysRemaining();
    echo "License expires in {$daysLeft} days";
}

protected $middlewareAliases = [
    'license' => \Nazrulalif\LaravelPemLicenseValidator\Middleware\ValidateLicense::class,
];

Route::middleware('license')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index']);
    Route::get('/settings', [SettingsController::class, 'index']);
});

public function boot()
{
    try {
        $validator = app(\Nazrulalif\LaravelPemLicenseValidator\LicenseValidator::class);

        if (!$validator->isValid()) {
            abort(403, 'Invalid or expired license: ' . $validator->getError());
        }

        // Check grace period
        if ($validator->isInGracePeriod()) {
            logger()->warning('License in grace period. Days remaining: ' . $validator->getDaysRemaining());
        }

    } catch (\Exception $e) {
        abort(500, 'License validation error: ' . $e->getMessage());
    }
}

use Nazrulalif\LaravelPemLicenseValidator\Facades\License;

// Check if feature exists
if (License::hasFeature('api_access')) {
    // Enable API routes
}

// Get feature value with default
$maxUsers = License::getFeature('UserLimit', 10);

// Get all features
$features = License::getLicenseData()['features'];

// Validate license
$validator->validate(): array

// Check if valid
$validator->isValid(): bool

// Get license data
$validator->getLicenseData(): array

// Get days remaining
$validator->getDaysRemaining(): int

// Check grace period
$validator->isInGracePeriod(): bool

// Get feature value
$validator->getFeature(string $key, mixed $default = null): mixed

// Check feature exists
$validator->hasFeature(string $key): bool

// Get error message
$validator->getError(): ?string

use Nazrulalif\LaravelPemLicenseValidator\Exceptions\LicenseException;

try {
    $validator->validate();
} catch (LicenseException $e) {
    switch ($e->getCode()) {
        case LicenseException::FILE_NOT_FOUND:
            // Handle missing file
            break;
        case LicenseException::INVALID_SIGNATURE:
            // Handle tampered license
            break;
        case LicenseException::EXPIRED:
            // Handle expiry
            break;
        case LicenseException::HARDWARE_MISMATCH:
            // Handle hardware binding
            break;
        case LicenseException::PRODUCT_KEY_MISMATCH:
            // Handle product key validation failure
            break;
    }
}

License::clearCache();
bash
php artisan vendor:publish --provider="Nazrulalif\LaravelPemLicenseValidator\LicenseServiceProvider"
json
{
  "productKey": "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX",
  "licenseId": "LIC-1234567890",
  "licenseType": "PROFESSIONAL",
  "companyName": "Acme Corporation",
  "email": "[email protected]",
  "maxDevices": 5,
  "features": {
    "UserLimit": 50,
    "sso": true,
    "api_access": true,
    "storage_gb": 100
  },
  "hardwareId": null,
  "issueDate": "2025-01-01T00:00:00Z",
  "expiryDate": "2026-01-01T00:00:00Z"
}