Laravel License Key System -
$licenseKey = $request->header('X-License-Key') ?? config('app.license_key'); if (!$licenseKey) return response()->json(['error' => 'License key required'], 401);
$result = (new LicenseService)->validate($licenseKey, $request->getHost());
Register in kernel.php and use in routes:
$license = License::where('key', $key)->first(); laravel license key system
if ($activeDomains >= $license->max_domains) // allow if this domain is already activated return $license->activations()->where('domain', $domain)->exists();
$key = Str::upper(Str::random($segments * $charsPerSegment)); $formatted = implode('-', str_split($key, $charsPerSegment)); return $prefix ? $prefix . '-' . $formatted : $formatted;
$license = License::create([ 'key' => generateLicenseKey('PROD'), 'user_id' => auth()->id(), 'product_name' => 'Pro Plan', 'valid_until' => now()->addYear(), 'max_domains' => 3, 'features' => ['api', 'export'] ]); Create a LicenseService class. $licenseKey = $request->header('X-License-Key')
if ($license->status !== 'active') return ['valid' => false, 'message' => "License is $license->status."];
$response = Http::post('https://your-api.com/api/license/verify', [ 'license_key' => env('LICENSE_KEY'), 'domain' => url('/') ]); if (!$response->json('valid')) abort(403, $response->json('message'));
LicenseActivation::updateOrCreate( ['license_id' => $license->id, 'domain' => $domain], ['ip' => $ip, 'last_verified_at' => now()] ); '-' . $formatted : $formatted
Create CheckLicense middleware:
protected function checkDomainLimit(License $license, string $domain): bool
return true;
