Php License Key System Github Official

/** * Validate activation code */ private function validateActivation($licenseId, $activationCode) { $sql = "SELECT is_active FROM license_activations WHERE license_id = :license_id AND activation_code = :activation_code"; $stmt = $this->db->prepare($sql); $stmt->execute([ ':license_id' => $licenseId, ':activation_code' => $activationCode ]); $result = $stmt->fetch(); return $result && $result['is_active']; }

public function lastInsertId() { return $this->connection->lastInsertId(); } } <?php // src/LicenseGenerator.php require_once DIR . '/Database.php';

public function prepare($sql) { return $this->connection->prepare($sql); }

/** * Update last validated timestamp */ private function updateLastValidated($licenseId) { $sql = "UPDATE licenses SET last_validated_at = NOW() WHERE id = :id"; $stmt = $this->db->prepare($sql); $stmt->execute([':id' => $licenseId]); } php license key system github

/** * Create a unique license key */ private function createLicenseKey($productId) { $prefix = strtoupper(substr(preg_replace('/[^a-zA-Z0-9]/', '', $productId), 0, 4)); $uniqueId = uniqid() . bin2hex(random_bytes(8)); $hash = hash_hmac('sha256', $uniqueId . LICENSE_SALT, SECRET_KEY); $licenseKey = $prefix . '-' . strtoupper(substr($hash, 0, 8)) . '-' . strtoupper(substr($hash, 8, 8)) . '-' . strtoupper(substr($hash, 16, 8)); // Check for uniqueness $checkSql = "SELECT id FROM licenses WHERE license_key = :license_key"; $checkStmt = $this->db->prepare($checkSql); $checkStmt->execute([':license_key' => $licenseKey]); if ($checkStmt->rowCount() > 0) { return $this->createLicenseKey($productId); // Recursive retry } return $licenseKey; }

/** * Validate with remote server */ private function validateWithServer() { $ch = curl_init($this->apiUrl . '/validate.php'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ 'license_key' => $this->licenseKey, 'domain' => $this->domain, 'activation_code' => $this->activationCode ])); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200) { return ['valid' => false, 'error' => 'License server error']; } return json_decode($response, true); }

/** * Validate domain */ private function validateDomain($licenseId, $domain) { $sql = "SELECT COUNT(*) as count FROM license_domains WHERE license_id = :license_id AND domain = :domain"; $stmt = $this->db->prepare($sql); $stmt->execute([ ':license_id' => $licenseId, ':domain' => $domain ]); $result = $stmt->fetch(); return $result['count'] > 0; } /** * Validate activation code */ private function

// Usage example $client = new LicenseClient('https://your-license-server.com/api', 'YOUR-LICENSE-KEY', $_SERVER['HTTP_HOST']); $validation = $client->checkLicense();

/** * Get license by key */ private function getLicense($licenseKey) { $sql = "SELECT * FROM licenses WHERE license_key = :license_key"; $stmt = $this->db->prepare($sql); $stmt->execute([':license_key' => $licenseKey]); return $stmt->fetch(); }

// Security settings define('SECRET_KEY', 'your-super-secret-key-change-this'); define('ENCRYPTION_METHOD', 'AES-256-CBC'); define('LICENSE_SALT', 'unique-salt-for-license-generation'); LICENSE_SALT, SECRET_KEY); $licenseKey = $prefix

/** * Cache validation result */ private function cacheValidation($data) { file_put_contents($this->cacheFile, json_encode([ 'timestamp' => time(), 'data' => $data ])); }

/** * Log validation attempt */ private function logValidation($licenseId, $domain) { $sql = "INSERT INTO license_logs (license_id, action, details, ip_address) VALUES (:license_id, 'validation', :details, :ip_address)"; $stmt = $this->db->prepare($sql); $stmt->execute([ ':license_id' => $licenseId, ':details' => "Validation from domain: {$domain}", ':ip_address' => $_SERVER['REMOTE_ADDR'] ?? null ]); } } Generate License ( api/generate.php ) <?php // api/generate.php header('Content-Type: application/json'); require_once '../src/LicenseGenerator.php';

// License settings define('MAX_ACTIVATION_ATTEMPTS', 5); define('VALIDATION_TIMEOUT_HOURS', 24); <?php // src/Database.php class Database { private static $instance = null; private $connection;