简要说明
这是一个极简的 PHP 验证服务:verify.php 从查询参数 pass 接收密钥,并将其与 pass.txt 中列出的允许密钥逐行比较;匹配时返回 JSON {"status":"pass"},否则返回 {"status":"none"}。
文件说明
- verify.php:主要脚本,读取
pass.txt并对传入的pass参数进行安全比较(使用hash_equals)。 - pass.txt:允许通过的密钥列表,每行一个。请勿将真实密钥公开到版本控制中。
运行要求
PHP 5.6+(推荐 PHP 7+)
快速开始(开发/本地测试)
- 将仓库放在 Web 服务器可访问的目录,或在项目目录运行内置开发服务器:
php -S localhost:8000
- 在浏览器或使用
curl访问:
curl "http://localhost:8000/verify.php?pass=要验证的密钥"
# 返回示例:{"status":"pass"} 或 {"status":"none"}
行为与实现细节
verify.php会读取pass.txt的所有非空行,逐行使用hash_equals与传入的pass比较,匹配即视为通过并返回status: pass。- 如果未提供
pass参数,脚本将返回{"status":"none"}。
安全提醒(重要)
- 当前实现以明文形式在
pass.txt中存储密钥,不适合生产环境。请避免将pass.txt提交到公共仓库。 - 不要通过明文查询字符串在生产环境传输敏感密钥(GET 会被记录在日志)。建议改为使用 HTTPS + POST,并在服务器端存储密钥的哈希(例如使用
password_hash/password_verify或其他 HMAC 方案)。 - 为
pass.txt设置严格的文件权限(仅允许 Web 服务器用户读取),并将其放在 Web 根目录之外。
扩展建议
- 将密钥存储改为安全哈希或使用环境变量/配置服务。
- 如果需要更高的安全性,添加速率限制、IP 白名单与事件审计。
源码
verify.php
<?php
header('Content-Type: application/json');
// The path to the file containing the secret key.
$keyFile = __DIR__ . '/pass.txt';
// The response structure.
$response = ['status' => 'none'];
// Check if the key file exists and is readable.
if (file_exists($keyFile) && is_readable($keyFile)) {
// Read the key from the file and trim any whitespace.
$storedKey = trim(file_get_contents($keyFile));
// Get the key from the query parameter.
$submittedPass = isset($_GET['pass']) ? $_GET['pass'] : '';
// If no password provided, return failure.
if (empty($submittedPass)) {
echo json_encode(['status' => 'none']);
exit;
}
// Read all keys from the file.
$keys = file($keyFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$matchFound = false;
// Compare the provided key with the stored keys.
foreach ($keys as $key) {
// Use hash_equals for timing-attack-safe comparison.
if (hash_equals(trim($key), $submittedPass)) {
$matchFound = true;
break; // Found a match, exit loop.
}
}
// Set the response status.
if ($matchFound) {
$response['status'] = 'pass';
}
}
// Output the JSON response.
echo json_encode($response);
?>
正文完


