You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现仅允许通过带UID与交互计数器的NFC标签链接访问网页

Alright, let's break down how to build this access control system for your high-value content site. The goal is to only let users in who arrive via your NFC tags (with the UID and counter parameters), while sending everyone else to an exit page. Here's a step-by-step solution:

NFC-Based Access Control Implementation

Core Validation Logic

First, let's recap the critical checks your backend needs to perform to distinguish valid NFC visits from unauthorized ones:

  • Extract and parse the m parameter from the request URL, splitting it into the tag UID and counter value (split on the x character).
  • Verify the UID exists in your pre-approved tag whitelist (you should store all valid NFC tag UIDs in a database or config file—each tag has a unique, fixed UID).
  • Check that the incoming counter value is greater than the last recorded counter value for that UID—this is the key to blocking re-used links from browser history. Every NFC tag read increments the counter, so historical links will have a stale, lower counter value.
  • If both checks pass, redirect to your content page; if either fails, send users to the exit page.

Code Example (PHP)

<?php
// 模拟存储有效标签UID与对应最后计数器值的数据库(实际部署请用真实数据库持久化)
$validTags = [
    "unique_tag_UID_001" => 0,
    "unique_tag_UID_002" => 0
];

// 获取URL中的m参数
$accessParam = $_GET['m'] ?? '';

// 拆分UID与计数器值
$paramParts = explode('x', $accessParam);
if (count($paramParts) !== 2) {
    // 参数格式无效,跳转至退出页
    header("Location: /exit-page.php");
    exit;
}

$tagUid = $paramParts[0];
$currentCounter = (int)$paramParts[1];

// 验证UID有效性与计数器递增逻辑
if (isset($validTags[$tagUid]) && $currentCounter > $validTags[$tagUid]) {
    // 更新该标签的最后计数器值(实际要写入数据库)
    $validTags[$tagUid] = $currentCounter;
    // 跳转至内容页
    header("Location: /content-page.php");
    exit;
} else {
    // 验证失败,跳转至退出页
    header("Location: /exit-page.php");
    exit;
}
?>

Code Example (Node.js/Express)

const express = require('express');
const app = express();

// 模拟存储有效标签的内存数据库(实际部署请用MongoDB/MySQL等持久化存储)
let validTags = {
    "unique_tag_UID_001": 0,
    "unique_tag_UID_002": 0
};

app.get('/', (req, res) => {
    const accessParam = req.query.m;
    
    // 无参数直接跳转退出页
    if (!accessParam) {
        return res.redirect('/exit-page');
    }

    const [tagUid, counterStr] = accessParam.split('x');
    const currentCounter = parseInt(counterStr);

    // 参数格式验证失败
    if (!tagUid || isNaN(currentCounter)) {
        return res.redirect('/exit-page');
    }

    // 核心验证:UID有效且计数器递增
    if (validTags.hasOwnProperty(tagUid) && currentCounter > validTags[tagUid]) {
        // 更新计数器值
        validTags[tagUid] = currentCounter;
        return res.redirect('/content-page');
    } else {
        return res.redirect('/exit-page');
    }
});

app.listen(3000, () => console.log('Server running on port 3000'));

Key Notes for Production Deployment

  • Persistent Counter Storage: The examples use in-memory storage for simplicity—replace this with a real database (MySQL, MongoDB, etc.) to retain counter values across server restarts.
  • Tamper Resistance: While the incrementing counter blocks historical links, you can add extra safeguards like logging all counter values for each UID to ensure increments are sequential (prevents random high-value counter forgeries).
  • HTTPS: Always use HTTPS to protect the URL parameters from interception or tampering.
  • Edge Cases: Handle scenarios like malformed parameters, invalid UIDs, and counter values that aren't integers by redirecting to the exit page.

内容的提问来源于stack exchange,提问作者trop yes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 10:16:07