域名验证文档

Web 应用域名授权验证指南

功能说明

域名验证功能用于保护 Web 应用,确保只有已授权的域名才能正常使用系统。适用于 SaaS 平台、网站系统等场景。

验证流程

1

添加域名

用户在系统中添加需要授权的域名

2

DNS 验证(可选)

通过添加 TXT 记录验证域名所有权

3

激活域名

管理员审核或自动激活域名

4

API 验证

应用通过 API 验证域名授权状态

验证接口

请求地址:

GE../api/domains.php?action=verify&domain={域名}&product_id={产品ID}

请求参数:

参数 说明 必填
action固定值 verify
domain要验证的域名
product_id产品ID

返回示例:

{
  "success": true,
  "data": {
    "domain": "example.com",
    "status": "active",
    "expires_at": "2025-12-31 23:59:59",
    "product": {
      "id": 1,
      "name": "产品名称"
    }
  }
}

集成代码示例

PHP 验证示例:

<?php
function verifyDomain($domain, $productId) {
    $apiUrl = 'https://your-domain.c../api/domains.php';
    $params = http_build_query([
        'action' => 'verify',
        'domain' => $domain,
        'product_id' => $productId
    ]);
    
    $response = file_get_contents($apiUrl . '?' . $params);
    $result = json_decode($response, true);
    
    return $result['success'] && $result['data']['status'] === 'active';
}

// 使用示例
$currentDomain = $_SERVER['HTTP_HOST'];
if (!verifyDomain($currentDomain, 1)) {
    die('域名未授权');
}

JavaScript 验证示例:

async function verifyDomain(domain, productId) {
    const params = new URLSearchParams({
        action: 'verify',
        domain: domain,
        product_id: productId
    });
    
    const response = await fetch(\`https://your-domain.c../api/domains.php?\${params}\`);
    const result = await response.json();
    
    return result.success && result.data?.status === 'active';
}

// 使用示例
verifyDomain(window.location.hostname, 1).then(valid => {
    if (!valid) {
        document.body.innerHTML = '<h1>域名未授权</h1>';
    }
});

注意事项