session_start();
$k = $_GET['k'];
$t = $_GET['t'];
$allowTime = 1800; // 防刷新有效期(秒)
$ip = get_client_ip();
$allowT = md5($ip . $k . $t);
if (!isset($_SESSION[$allowT])) {
$refresh = true;
$_SESSION[$allowT] = time();
} elseif (time() - $_SESSION[$allowT] > $allowTime) {
$refresh = true;
$_SESSION[$allowT] = time();
} else {
$refresh = false; // 重複刷新
}
if (isset($_POST)) {
// 1. 接收驗證數據
// 2. 業務處理
// 3. 成功後重定向到自身(GET)
header("Location: " . $_SERVER['PHP_SELF']);
exit;
}
// 4. 頁面顯示邏輯
// 頁面頭部加入 JS 防止瀏覽器後退重提交
echo '<script>javascript:window.history.forward(1);</script>';
使用 <input type="image"> 代替 submit 時,圖片上的 onclick 可能導致雙重提交,改用普通 <input type="submit"> 可避免。