本文目录导读:

在 PHP 中实现“在线激活”通常指的是软件授权验证功能,常见的实现方式有以下几种:
基于密钥验证(最基础)
<?php
// 生成有效许可证(服务器端)
function generateLicense($productKey, $username, $expireDays) {
$secret = "YOUR_SECRET_KEY";
$data = $productKey . $username . date('Y-m-d', strtotime("+$expireDays days"));
return hash_hmac('sha256', $data, $secret);
}
// 验证许可证
function verifyLicense($license, $productKey, $username) {
$secret = "YOUR_SECRET_KEY";
$data = $productKey . $username . date('Y-m-d');
$expected = hash_hmac('sha256', $data, $secret);
// 清理过期许可证
return hash_equals($expected, $license);
}
远程服务器激活(推荐)
客户端激活代码:
<?php
class LicenseActivator {
private $serverUrl = 'https://your-server.com/api/activate.php';
public function activate($serialKey, $domain) {
$data = [
'serial_key' => $serialKey,
'domain' => $domain,
'server_ip' => $_SERVER['SERVER_ADDR']
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->serverUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_SSL_VERIFYPEER => false
]);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
if ($result['status'] === 'success') {
// 存储激活信息
$this->saveActivation($result['license']);
return true;
}
return false;
}
private function saveActivation($license) {
$config = "<?php return " . var_export([
'license_key' => $license,
'activation_date' => date('Y-m-d H:i:s'),
'expire_date' => date('Y-m-d H:i:s', strtotime('+365 days'))
], true) . ";";
file_put_contents('../config/license.php', $config);
}
}
服务器端验证代码:
<?php
// /api/activate.php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$serialKey = $_POST['serial_key'];
$domain = $_POST['domain'];
$serverIp = $_SERVER['REMOTE_ADDR'];
// 检查数据库中的序列号
$db = new PDO('mysql:host=localhost;dbname=license_db', 'user', 'pass');
$stmt = $db->prepare("SELECT * FROM licenses WHERE serial_key = ?");
$stmt->execute([$serialKey]);
$license = $stmt->fetch(PDO::FETCH_ASSOC);
if ($license && $license['status'] == 'available') {
// 生成激活码
$activationCode = md5($serialKey . $serverIp . time());
// 更新许可证状态
$update = $db->prepare("UPDATE licenses SET
activation_code = ?,
domain = ?,
activated_ip = ?,
activated_at = NOW(),
expire_at = DATE_ADD(NOW(), INTERVAL 365 DAY),
status = 'activated'
WHERE id = ?");
$update->execute([$activationCode, $domain, $serverIp, $license['id']]);
echo json_encode([
'status' => 'success',
'license' => $activationCode,
'expire_date' => date('Y-m-d', strtotime('+365 days'))
]);
} else {
echo json_encode(['status' => 'error', 'message' => 'Invalid or used license']);
}
}
离线激活(无需服务器)
<?php
// 获取系统信息生成机器码
function getMachineCode() {
$systemInfo = php_uname() . '/' .
(isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '') . '/' .
gethostname();
return md5($systemInfo);
}
// 激活验证(离线)
function verifyOfflineLicense($licenseFile) {
if (!file_exists($licenseFile)) {
return false;
}
$license = file_get_contents($licenseFile);
$data = json_decode($license, true);
// 验证机器码
if ($data['machine_code'] !== getMachineCode()) {
return false;
}
// 验证有效期
if (strtotime($data['expire_date']) < time()) {
return false;
}
return true;
}
加密授权文件(更安全)
<?php
// 生成激活文件
function generateLicenseFile($data) {
$publicKey = file_get_contents('public.pem');
// 使用公钥加密
$encryptedData = null;
openssl_public_encrypt($data, $encryptedData, $publicKey);
return base64_encode($encryptedData);
}
// 验证激活文件(客户端)
function verifyEncryptedLicense($encryptedContent) {
$privateKey = file_get_contents('private.pem');
// 解密内容
$decryptedData = null;
openssl_private_decrypt(base64_decode($encryptedContent), $decryptedData, $privateKey);
return unserialize($decryptedData);
}
完整示例:带登录验证的激活系统
<?php
class ActivationSystem {
private $db;
public function __construct() {
// 数据库连接
$this->db = new mysqli('host', 'user', 'pass', 'software_license');
}
// 注册并激活
public function registerUser($username, $email, $password, $serialKey) {
// 先注册用户
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
$this->db->query("INSERT INTO users (username, email, password, license_key)
VALUES ('$username', '$email', '$hashedPassword', '$serialKey')");
// 生成激活码
$activationCode = bin2hex(random_bytes(16));
$this->db->query("INSERT INTO activations
(user_id, activation_code, created_at, is_active)
VALUES (LAST_INSERT_ID(), '$activationCode', NOW(), 1)");
// 发送激活邮件
$this->sendActivationEmail($email, $activationCode);
return true;
}
// 邮件发送函数
private function sendActivationEmail($email, $code) {
$subject = "软件激活";
$message = "您的激活码:$code,请点击链接激活:https://yourdomain.com/activate.php?code=$code";
mail($email, $subject, $message);
}
// 激活检测
public function checkActivation() {
session_start();
if (isset($_SESSION['user_id'])) {
$userId = $_SESSION['user_id'];
$result = $this->db->query("SELECT * FROM activations WHERE user_id = $userId AND is_active = 1");
return $result->num_rows > 0;
}
return false;
}
}
注意事项:
- 安全性:永远不要在前端暴露密钥,使用HTTPS
- 防破解:结合混淆、加密、签名等多种技术
- 用户体验:提供离线激活选项
- 错误处理:清晰的错误提示
- 日志记录:记录所有激活操作
选择哪种方案取决于你的需求:
- 简单授权:适合小项目,使用方案1或2
- 商业软件:建议使用方案2+4,配合用户注册系统
- 企业级:需要完整的授权服务器
我建议使用远程激活(方案2),因为它灵活、可维护性高,且易于控制授权数量。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播
英超直播