在 Web 開發中,對用戶輸入進行統一的獲取、過濾、類型轉換是安全與穩健的基礎。本文提供一個單例模式的 cls_request 類,封裝了對 $_GET、$_POST、$_REQUEST、$_COOKIE、$_FILES 的標準化處理。
trim 字符串,保留數組結構(如 checkbox)。get_num/post_num 等方法自動轉換為整型,非數字返回 false。get_string/post_string 等方法,默認調用 htmlspecialchars(ENT_QUOTES) 防 XSS,可選關閉。filedata() 直接返回 $_FILES 子項。<?php
class cls_request {
private $getdata, $postdata, $requestdata, $filedata, $cookiedata;
static private $_instance;
private function __construct() {
$this->getdata = self::format_data($_GET);
$this->postdata = self::format_data($_POST);
$this->requestdata = array_merge($this->getdata, $this->postdata);
$this->cookiedata = self::format_data($_COOKIE);
$this->filedata = self::format_data($_FILES);
}
public static function get_instance() {
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
// --- 數值型獲取 ---
public function get_num($key) { return $this->_get_typed($this->getdata, $key); }
public function post_num($key) { return $this->_get_typed($this->postdata, $key); }
public function request_num($key) { return $this->_get_typed($this->requestdata, $key); }
public function cookie_num($key) { return $this->_get_typed($this->cookiedata, $key); }
private function _get_typed($source, $key) {
if (!isset($source[$key])) return false;
return $this->to_num($source[$key]);
}
// --- 字符串獲取 ---
public function get_string($key, $filter = true) { return $this->_get_str($this->getdata, $key, $filter); }
public function post_string($key, $filter = true) { return $this->_get_str($this->postdata, $key, $filter); }
public function request_string($key, $filter = true) { return $this->_get_str($this->requestdata, $key, $filter); }
public function cookie_string($key, $filter = true) { return $this->_get_str($this->cookiedata, $key, $filter); }
private function _get_str($source, $key, $filter) {
if (!isset($source[$key])) return false;
return $filter ? $this->filter_string($source[$key]) : $source[$key];
}
public function filedata($key) { return isset($this->filedata[$key]) ? $this->filedata[$key] : null; }
// --- 內部工具方法 ---
private static function format_data($data) {
$result = array();
if (!is_array($data)) return $result;
foreach ($data as $key => $value) {
if (is_array($value)) {
$result[$key] = $value; // 保留數組(如 checkbox[])
} else {
$result[$key] = trim($value);
}
}
return $result;
}
private function to_num($num) {
return is_numeric($num) ? intval($num) : false;
}
private function filter_string($data) {
if ($data === null) return false;
if (is_array($data)) {
foreach ($data as $k => $v) $data[$k] = htmlspecialchars($v, ENT_QUOTES);
return $data;
}
return htmlspecialchars($data, ENT_QUOTES);
}
}
?>
<?php
require 'cls_request.php';
$req = cls_request::get_instance();
// 獲取整型 ID,自動過濾非數字
$id = $req->get_num('id');
if ($id === false) exit('非法 ID');
// 獲取過濾後的字符串(防 XSS)
$username = $req->post_string('username');
// 獲取原始字符串(不轉義,如富文本編輯器內容)
$content = $req->post_string('content', false);
// 文件上傳信息
$fileInfo = $req->filedata('avatar');
?>
htmlspecialchars 僅防輸出型 XSS,數據入庫前仍需使用預處理語句防注入。$_REQUEST 合併順序受 request_order 配置影響,建議顯式使用 GET/POST 方法以避免歧義。