License Key System Github - Php
/** * 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';
$data = json_decode(file_get_contents('php://input'), true);
echo json_encode(['success' => true, 'data' => $result]); <?php // api/validate.php header('Content-Type: application/json'); require_once '../src/LicenseValidator.php'; 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) );
/** * Check if license is valid */ public function checkLicense() { // Check cache first $cached = $this->getCachedValidation(); if ($cached && $cached['timestamp'] > (time() - 86400)) { // 24 hour cache return $cached['data']; } // Validate with server $result = $this->validateWithServer(); if ($result['valid']) { $this->cacheValidation($result); } return $result; } /** * Log validation attempt */ private function
public function prepare($sql) { return $this->connection->prepare($sql); }
// License settings define('MAX_ACTIVATION_ATTEMPTS', 5); define('VALIDATION_TIMEOUT_HOURS', 24); <?php // src/Database.php class Database { private static $instance = null; private $connection; null ); class LicenseGenerator { private $db; /**
$generator = new LicenseGenerator(); $result = $generator->generateLicenseKey( $data['product_id'], $data['customer_name'], $data['customer_email'], $data['license_type'], $data['max_domains'] ?? 1, $data['expiry_days'] ?? null );
class LicenseGenerator { private $db;
/** * 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; }
// Usage example $client = new LicenseClient('https://your-license-server.com/api', 'YOUR-LICENSE-KEY', $_SERVER['HTTP_HOST']); $validation = $client->checkLicense();