Php License Key System Github [2026 Edition]
/** * Get cached validation */ private function getCachedValidation() { if (file_exists($this->cacheFile)) { $cache = json_decode(file_get_contents($this->cacheFile), true); return $cache; } return null; }
-- License logs table CREATE TABLE IF NOT EXISTS license_logs ( id INT AUTO_INCREMENT PRIMARY KEY, license_id INT, action VARCHAR(50), details TEXT, ip_address VARCHAR(45), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, INDEX idx_license_id (license_id), INDEX idx_action (action), INDEX idx_created_at (created_at) ); <?php // config/database.php define('DB_HOST', 'localhost'); define('DB_NAME', 'license_system'); define('DB_USER', 'your_username'); define('DB_PASS', 'your_password');
/** * 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; } php license key system github
/** * 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(); }
/** * Generate a unique license key */ public function generateLicenseKey($productId, $customerName, $customerEmail, $licenseType, $maxDomains = 1, $expiryDays = null) { // Generate unique license key $licenseKey = $this->createLicenseKey($productId); // Calculate expiry date $expiresAt = null; if ($expiryDays !== null) { $expiresAt = date('Y-m-d H:i:s', strtotime("+{$expiryDays} days")); } elseif ($licenseType !== 'perpetual') { $duration = $licenseType === 'trial' ? 30 : ($licenseType === 'monthly' ? 30 : 365); $expiresAt = date('Y-m-d H:i:s', strtotime("+{$duration} days")); } // Insert into database $sql = "INSERT INTO licenses (license_key, product_id, customer_name, customer_email, license_type, max_domains, expires_at) VALUES (:license_key, :product_id, :customer_name, :customer_email, :license_type, :max_domains, :expires_at)"; $stmt = $this->db->prepare($sql); $stmt->execute([ ':license_key' => $licenseKey, ':product_id' => $productId, ':customer_name' => $customerName, ':customer_email' => $customerEmail, ':license_type' => $licenseType, ':max_domains' => $maxDomains, ':expires_at' => $expiresAt ]); // Log the action $this->logAction($this->db->lastInsertId(), 'license_generated', "License generated for {$customerEmail}"); return [ 'license_key' => $licenseKey, 'expires_at' => $expiresAt, 'license_type' => $licenseType ]; } /** * Get cached validation */ private function
$data = json_decode(file_get_contents('php://input'), true);
/** * 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']; } strtoupper(substr($hash, 8, 8))
/** * Cache validation result */ private function cacheValidation($data) { file_put_contents($this->cacheFile, json_encode([ 'timestamp' => time(), 'data' => $data ])); }