Php License Key: System Github
-- License activations table (track activations) CREATE TABLE IF NOT EXISTS license_activations ( id INT AUTO_INCREMENT PRIMARY KEY, license_id INT NOT NULL, activation_code VARCHAR(64) UNIQUE NOT NULL, ip_address VARCHAR(45), user_agent TEXT, activated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, last_ping_at TIMESTAMP NULL, is_active BOOLEAN DEFAULT TRUE, FOREIGN KEY (license_id) REFERENCES licenses(id) ON DELETE CASCADE, INDEX idx_activation_code (activation_code), INDEX idx_license_active (license_id, is_active) );
/** * Validate a license key */ public function validate($licenseKey, $domain = null, $activationCode = null) { // Get license details $license = $this->getLicense($licenseKey); if (!$license) { return ['valid' => false, 'error' => 'Invalid license key']; } // Check status if ($license['status'] !== 'active') { return ['valid' => false, 'error' => "License is {$license['status']}"]; } // Check expiry if ($license['expires_at'] && strtotime($license['expires_at']) < time()) { $this->updateLicenseStatus($license['id'], 'expired'); return ['valid' => false, 'error' => 'License has expired']; } // Validate domain if provided if ($domain && !$this->validateDomain($license['id'], $domain)) { return ['valid' => false, 'error' => 'Domain not authorized for this license']; } // Validate activation if provided if ($activationCode && !$this->validateActivation($license['id'], $activationCode)) { return ['valid' => false, 'error' => 'Invalid activation code']; } // Update last validated timestamp $this->updateLastValidated($license['id']); // Log validation $this->logValidation($license['id'], $domain); return [ 'valid' => true, 'license_type' => $license['license_type'], 'expires_at' => $license['expires_at'], 'max_domains' => $license['max_domains'], 'customer_name' => $license['customer_name'], 'customer_email' => $license['customer_email'] ]; } php license key system github
/** * 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 ]; } null ]); } } <
/** * 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; } } } <
/** * Log license actions */ private function logAction($licenseId, $action, $details) { $sql = "INSERT INTO license_logs (license_id, action, details, ip_address) VALUES (:license_id, :action, :details, :ip_address)"; $stmt = $this->db->prepare($sql); $stmt->execute([ ':license_id' => $licenseId, ':action' => $action, ':details' => $details, ':ip_address' => $_SERVER['REMOTE_ADDR'] ?? null ]); } } <?php // src/LicenseValidator.php require_once DIR . '/Database.php';
$data = json_decode(file_get_contents('php://input'), true);
$required = ['product_id', 'customer_name', 'customer_email', 'license_type']; foreach ($required as $field) { if (empty($data[$field])) { http_response_code(400); echo json_encode(['error' => "Missing field: {$field}"]); exit; } }