靜態頁無法直接執行 PHP,需採用 AJAX/JS 跨域請求動態接口 方案:頁面靜態化時預留容器,前端腳本讀取登錄 Cookie,請求 /e/api/check_perm.php 獲取權限標識,再決定顯示/隱藏/跳轉。
/e/api/check_perm.php<?php
// 安全頭
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN'] ?? '*');
header('Access-Control-Allow-Credentials: true');
require_once '../../class/connect.php';
require_once '../../class/db_sql.php';
require_once '../../class/functions.php';
require_once '../../data/dbcache/class.php';
require_once '../../data/dbcache/MemberLevel.php';
$link = db_connect();
$empire = new mysqlquery();
// 1. 獲取會員組 ID(優先 Cookie,其次 Session)
$mluserid = (int)getcvar('mluserid', 1); // 1=cookie
$mlgroupid = (int)getcvar('mlgroupid', 1);
$mlusername = RepPostVar(getcvar('mlusername', 1));
// 2. 參數校驗
$classid = (int)$_GET['classid'];
$id = (int)$_GET['id'];
if (!$classid || !$id) { json_exit(['code' => 400, 'msg' => '參數缺失']); }
// 3. 讀取內容基本信息(可緩存)
$tbname = $class_r[$classid]['tbname'];
$r = $empire->fetch1("SELECT id, title, newstext FROM {$dbtbpre}ecms_{$tbname} WHERE id=$id AND classid=$classid LIMIT 1");
if (!$r) { json_exit(['code' => 404, 'msg' => '內容不存在']); }
// 4. 權限判斷邏輯(示例:組 ID 1=遊客/註冊會員,2=VIP,3=SVIP)
$perm = [
'can_view' => false,
'level_name' => '遊客',
'redirect_url' => $public_r['newsurl'] . 'e/member/login.php'
];
switch ($mlgroupid) {
case 1: $perm = ['can_view' => false, 'level_name' => '註冊會員', 'msg' => '請升級 VIP 查看']; break;
case 2: $perm = ['can_view' => true, 'level_name' => 'VIP', 'title' => $r['title'], 'content' => $r['newstext']]; break;
case 3: $perm = ['can_view' => true, 'level_name' => 'SVIP', 'title' => $r['title'], 'content' => $r['newstext']]; break;
default: $perm['redirect_url'] = $public_r['newsurl'] . 'e/member/login.php';
}
// 5. 輸出 JSON
json_exit(['code' => 200, 'data' => $perm]);
function json_exit($arr) {
echo json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
db_close();
$GLOBALS['empire'] = null;
exit;
}
?>
<!-- 內容容器 -->
<div id="article-content" style="display:none;"></div>
<div id="article-prompt" class="alert"></div>
<script>
// 配置接口地址
const API_URL = '[!--news.url--]e/api/check_perm.php?classid=[!--classid--]&id=[!--id--]';
async function loadContent() {
try {
const res = await fetch(API_URL, { credentials: 'include' });
const json = await res.json();
if (json.code !== 200) throw new Error(json.msg);
const { can_view, level_name, title, content, msg, redirect_url } = json.data;
const box = document.getElementById('article-content');
const prompt = document.getElementById('article-prompt');
if (can_view) {
document.title = title + ' - [!--pagetitle--]';
box.innerHTML = content;
box.style.display = 'block';
} else {
prompt.className = 'alert alert-warning';
prompt.innerHTML = `當前權限:${level_name}。${msg || '內容僅限高級會員查看。'} <a href="${redirect_url}">去登錄/升級</a>`;
}
} catch (e) {
document.getElementById('article-prompt').innerHTML = '權限驗證失敗:' + e.message;
}
}
// 頁面加載即執行
loadContent();
</script>
User-Agent 白名單或預渲染服務。| 風險 | 對策 |
|---|---|
| Cookie 劫持/竊取 | 設置 HttpOnly; Secure; SameSite=Lax;全站 HTTPS |
| 接口被枚舉遍歷 | 簽名驗證:sign=md5(classid+id+timestamp+secret);限流 |
| 內容在網絡面板明文可見 | 敏感頁面改用 動態頁 或 加密傳輸(AES-GCM 前端解密) |
| CSRF | GET 請求無副作用,天然免疫;若有寫操作需加 Token |
// 擴展字段權限映射表
$fieldPerm = [
'newstext' => [2, 3], // VIP/SVIP 可看正文
'downurl' => [3], // 僅 SVIP 可下載附件
'price' => [0, 1, 2, 3] // 所有人可看價格
];
// 前端按字段渲染
if (can_view) {
box.innerHTML = content;
if (perm.includes(3)) box.querySelectorAll('.down-btn').forEach(b => b.style.display='');
}
適用版本:EmpireCMS 7.5/8.0,PHP 7.4+。生產環境請嚴格做好接口簽名、限流、日誌審計。