<?php // license.php function generateLicense($productId, $customerEmail) $data = $productId . $customerEmail . time(); $hash = substr(hash('sha256', $data), 0, 16); return implode('-', str_split(strtoupper($hash), 4));// Validate license function validateLicense($license, $secretKey) return hash_hmac('sha256', $license, $secretKey) === $license;
// Usage $license = generateLicense('PROD123', 'user@example.com'); echo "License Key: " . $license;
Now you need to implement the client script. Most repositories include a sample client.php in the examples/ folder. A basic validation function looks like this:
<?php function validateLicense($licenseKey, $productId, $apiUrl, $productSecret) $ch = curl_init($apiUrl . '/validate'); $payload = json_encode([ 'license_key' => $licenseKey, 'product_id' => $productId, 'domain' => $_SERVER['HTTP_HOST'], 'ip' => $_SERVER['REMOTE_ADDR'] ]);curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'X-Product-Secret: ' . $productSecret]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Always verify in production $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode === 200) $data = json_decode($response, true); return $data['valid'] === true; return false;
// Usage (place at start of your script) if (!validateLicense('USER-ENTERED-KEY', 1, 'https://yourdomain.com/license-system/api', 'your-product-secret')) die("Invalid license. Please purchase a valid license for this software.");
GitHub is vast, so finding the right project is the first step. Search for terms like "PHP License Manager," "PHP License System," or "Laravel License Manager" (if you prefer the Laravel framework).
What to look for:
For this tutorial, we will assume a standard structure common to most popular PHP licensing repositories (like PHP-License-Manager or similar projects).