將表單 HTML 與 PHP 處理邏輯合併在同一檔案,利用 $_SERVER['REQUEST_METHOD'] 區分 GET (顯示表單) 與 POST (處理提交),是簡單專案的經典模式。
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title>會員註冊系統</title>
<style>
.error { color: #c00; margin: 4px 0; }
.success { color: #080; margin: 10px 0; }
form { max-width: 320px; margin: 2rem auto; }
label { display: block; margin: 8px 0 4px; }
input[type="text"], input[type="password"] { width: 100%; padding: 8px; box-sizing: border-box; }
button { margin-top: 12px; padding: 8px 16px; }
</style>
</head>
<body>
<?php
// 處理 POST 提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$errors = [];
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
$agree = isset($_POST['agree']);
if ($username === '') {
$errors[] = '請輸入使用者名稱';
}
if ($password === '') {
$errors[] = '請輸入密碼';
} elseif (strlen($password) < 8) {
$errors[] = '密碼長度至少 8 碼';
}
if (!$agree) {
$errors[] = '請同意服務條款與隱私權政策';
}
if (empty($errors)) {
// 這裡通常:雜湊密碼、寫入資料庫、發送驗證信...
// 演示直接顯示成功 (實務勿明文顯示密碼!)
echo '註冊成功!
';
echo '歡迎 ' . htmlspecialchars($username) . ',帳號已建立。';
echo '';
} else {
// 顯示錯誤,表單保留輸入值
foreach ($errors as $err) {
echo '' . htmlspecialchars($err) . '';
}
}
}
?>
<h2>會員註冊</h2>
<form method="post" action="" novalidate>
<label>
使用者名稱:
<input type="text" name="username" value="<?= htmlspecialchars($username ?? '') ?>" required autocomplete="username">
</label>
<label>
密碼:
<input type="password" name="password" required autocomplete="new-password" minlength="8">
</label>
<label>
<input type="checkbox" name="agree" <?= isset($agree) && $agree ? 'checked' : '' ?>>
我已閱讀並同意《服務條款》與《隱私權政策》
</label>
<button type="submit">建立帳號</button>
</form>
</body>
</html>
// 推薦:明確判斷 REQUEST_METHOD
if ($_SERVER['REQUEST_METHOD'] === 'POST') { ... }
// 也可用:檢查特定欄位
if (isset($_POST['submit'])) { ... } // 但較不語意化
// PHP 7+ null 合併運算子
$username = trim($_POST['username'] ?? '');
// 舊版相容
$username = isset($_POST['username']) ? trim($_POST['username']) : '';
| 值 | isset() | empty() | 建議用法 |
|---|---|---|---|
| 未定義 / null | false | true | empty 檢查「無實質值」 |
| "" (空字串) | true | true | |
| "0" (字串零) | true | true | ⚠️ 注意:empty("0") 為 true |
| 0 (整數) | true | true | |
| false | true | true | |
| [] (空陣列) | true | true | |
| "false" (字串) | true | false | 非空字串! |
建議:檢查「使用者有輸入內容」用 !empty(trim($input));檢查「欄位存在」用 isset() 或 array_key_exists()。
<?= htmlspecialchars($username, ENT_QUOTES, 'UTF-8') ?>
// 或簡寫函數
function h(string $s): string { return htmlspecialchars($s, ENT_QUOTES, 'UTF-8'); }
<?= h($username) ?>
<?php
session_start();
// 產生 Token
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
// 驗證 Token
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'] ?? '')) {
die('CSRF 驗證失敗');
}
// ... 處理表單
}
?>
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
成功處理 POST 後 重導向到 GET 頁面,防止重新整理重複提交:
if (empty($errors)) {
// 處理註冊邏輯...
header('Location: register.php?success=1');
exit;
}