表單提交是 Web 交互的基石。PHP 提供了三個超全局變數 $_GET、$_POST、$_REQUEST 來接收客戶端數據。本文系統梳理三者差異、使用場景、安全風險與最佳實踐,助你構建健壯的表單處理邏輯。
| HTTP 方法 | PHP 超全局變數 | 數據位置 | 特點 |
|---|---|---|---|
| GET | $_GET | URL 查詢字符串 | 可見、可收藏、長度受限、非幂等操作不適用 |
| POST | $_POST | 請求體 | 不可見、無長度限制、適合修改數據、上傳文件 |
| GET/POST/COOKIE | $_REQUEST | 三者合併 | 便利但不安全、來源不明確、生產環境不推薦 |
<!-- get_form.html -->
<form action="get_handler.php" method="get">
用戶名:<input type="text" name="username" /><br/>
<input type="submit" value="提交" />
</form>
<?php
// get_handler.php
// 1. 獲取數據(建議檢查是否存在)
$username = $_GET['username'] ?? '';
// 2. 安全輸出(防 XSS)
echo '您輸入的用戶名:' . htmlspecialchars($username, ENT_QUOTES, 'UTF-8');
// 3. URL 實際樣貌:get_handler.php?username=Jacky
?>
<!-- post_form.html -->
<form action="post_handler.php" method="post">
用戶名:<input type="text" name="username" /><br/>
密碼:<input type="password" name="password" /><br/>
<input type="submit" value="登錄" />
</form>
<?php
// post_handler.php
// 1. 僅接受 POST 請求
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
exit('Method Not Allowed');
}
// 2. 獲取並過濾數據
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
// 3. 驗證
if ($username === '' || $password === '') {
exit('用戶名和密碼不能為空');
}
// 4. 業務邏輯(如驗證登錄、寫入資料庫)
// ...
echo '歡迎,' . htmlspecialchars($username, ENT_QUOTES, 'UTF-8');
?>
enctype="multipart/form-data")<?php
// 同時獲取 GET/POST/COOKIE,優先順序受 variables_order 影響(預設 EGPCS)
$name = $_REQUEST['name'] ?? ''; // 來源不明確!
// 風險示例:
// 用戶訪問:page.php?name=attacker
// 表單 POST 提交:name=victim
// $_REQUEST['name'] 可能取到 'attacker'(GET 覆蓋 POST)或 'victim',取決於配置
?>
| 表單元素 | HTML 示例 | PHP 獲取方式 | 返回值類型 |
|---|---|---|---|
| 文本框 | <input name="username"> | $_POST['username'] | 字符串 |
| 密碼框 | <input type="password" name="pwd"> | $_POST['pwd'] | 字符串 |
| 單選框 | <input type="radio" name="sex" value="1"> | $_POST['sex'] | 字符串(選中的 value) |
| 複選框(單個) | <input type="checkbox" name="agree" value="1"> | isset($_POST['agree']) | 布爾/字符串 |
| 複選框(多個) | <input type="checkbox" name="hobby[]" value="1"> | $_POST['hobby'] | 陣列 |
| 下拉選單 | <select name="city"><option value="bj">北京</option></select> | $_POST['city'] | 字符串 |
| 多選下拉 | <select name="city[]" multiple>...</select> | $_POST['city'] | 陣列 |
| 文件上傳 | <input type="file" name="avatar"> | $_FILES['avatar'] | 陣列(含 tmp_name, name, size, error, type) |
$_GET 取查詢參數,用 $_POST 取表單提交,禁用 $_REQUEST。$_SERVER['REQUEST_METHOD']。$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 120]]);
htmlspecialchars($str, ENT_QUOTES, 'UTF-8')。<!-- 批量編輯用戶 -->
<form method="post">
<input type="hidden" name="users[1][id]" value="1" />
<input type="text" name="users[1][name]" value="Alice" />
<input type="email" name="users[1][email]" value="alice@example.com" />
<input type="hidden" name="users[2][id]" value="2" />
<input type="text" name="users[2][name]" value="Bob" />
<input type="email" name="users[2][email]" value="bob@example.com" />
<button type="submit">批量更新</button>
</form>
<?php
// 處理
foreach ($_POST['users'] as $userData) {
// $userData = ['id'=>1, 'name'=>'Alice', 'email'=>'alice@example.com']
// 更新數據庫...
}
?>
掌握表單數據接收的核心機制與安全規範,是 PHP 開發者的必修課。始終堅持「明確來源、嚴格驗證、安全輸出」三原則,才能構建經得起攻擊的 Web 應用。