Php License Key System Github | No Sign-up |
public function __construct() { $this->db = Database::getInstance(); }
// Authentication check (you should implement proper auth) $apiKey = $_SERVER['HTTP_X_API_KEY'] ?? null; if ($apiKey !== 'your-admin-api-key') { http_response_code(401); echo json_encode(['error' => 'Unauthorized']); exit; }
public function lastInsertId() { return $this->connection->lastInsertId(); } } <?php // src/LicenseGenerator.php require_once DIR . '/Database.php';
$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; } } php license key system github
class LicenseValidator { private $db;
echo json_encode($result); <?php // public/example-client.php class LicenseClient { private $apiUrl; private $licenseKey; private $domain; private $activationCode; private $cacheFile;
private function __construct() { try { $this->connection = new PDO( "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME . ";charset=utf8mb4", DB_USER, DB_PASS, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false ] ); } catch (PDOException $e) { die("Database connection failed: " . $e->getMessage()); } } DB_HOST
/** * 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']; }
php-license-key-system/ ├── config/ │ └── database.php ├── src/ │ ├── License.php │ ├── LicenseGenerator.php │ ├── LicenseValidator.php │ └── Database.php ├── api/ │ ├── generate.php │ ├── validate.php │ ├── activate.php │ └── deactivate.php ├── public/ │ └── example-client.php ├── sql/ │ └── schema.sql └── README.md 1. Database Schema ( sql/schema.sql ) CREATE DATABASE IF NOT EXISTS license_system; USE license_system; -- Licenses table CREATE TABLE IF NOT EXISTS licenses ( id INT AUTO_INCREMENT PRIMARY KEY, license_key VARCHAR(64) UNIQUE NOT NULL, product_id VARCHAR(50) NOT NULL, customer_name VARCHAR(100), customer_email VARCHAR(100), license_type ENUM('trial', 'monthly', 'yearly', 'perpetual') DEFAULT 'monthly', max_domains INT DEFAULT 1, status ENUM('active', 'inactive', 'expired', 'suspended') DEFAULT 'active', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, expires_at TIMESTAMP NULL, last_validated_at TIMESTAMP NULL, INDEX idx_license_key (license_key), INDEX idx_status (status), INDEX idx_expires_at (expires_at) );
1. **HTTPS Required**: Always use HTTPS in production 2. **API Authentication**: Implement proper JWT or OAuth for admin endpoints 3. **Rate Limiting**: Add rate limiting to prevent abuse 4. **Regular Backups**: Backup the license database regularly 5. **Monitoring**: Monitor license validation logs for suspicious activity if ($cached && $cached['timestamp'] >
/** * 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; }
-- License domains table (for domain restrictions) CREATE TABLE IF NOT EXISTS license_domains ( id INT AUTO_INCREMENT PRIMARY KEY, license_id INT NOT NULL, domain VARCHAR(255) NOT NULL, activated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (license_id) REFERENCES licenses(id) ON DELETE CASCADE, UNIQUE KEY unique_domain_license (license_id, domain), INDEX idx_domain (domain) );